Java - How to handle very similar classes with nested classes

Hi All,

 I am having difficulties to handle the following classes gracefully:

Suppose I have the following class A:

class A{

	B b1;
	
	class B{
		
		C c1;
		D d1;
		
		class C{
		.
		.
		.
		}
		
		class D{
		.
		.
		.
		}
	
	}
}

Open in new window


and I have also class AA:

class AA{

	B b1;
	
	class B{
		
		C c1;
		E e1;
		
		class C{
		.
		.
		.
		}
		
		class E{
		.
		.
		.
		}
	
	}
}

Open in new window



I need to create and initialize objects of A or AA depending on some flag state.
Notice that class B inside class A is nesting class D whereas class B inside class AA is nesting class E !

The automatic thought I had is to derive them from some base class and use a factory.
Then I would get an object which I can use, but then I would face the need to work with D or E objects.
But I don't know exactly how to implement this idea. Is it possible at all? Is there a better way of achieving it?

Thank you!
Tal TeneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SurranoSystem EngineerCommented:
Hello Tal,

Sounds pretty obscure but derivation may work. However A:B and AA:B are not the same and the two C's aren't the same either.

Have the two B's implement the same interface that contains one or two initialisation functions. Is it something you are looking for?

The hardcore way is to exploit reflection but hard to tell more from this skeleton.


S.
1
Tal TeneAuthor Commented:
Thank you Surrano.
I didn't exactly understand your idea. If you could add a code example I'll be grateful.
The C classes are the same (contrary to what you said), but the B's are not because one of them includes a D object and the other an E object.
0
SurranoSystem EngineerCommented:
I doubt that you could assign an object of type A:B:C to a variable of type AA:B:C even if they were public.

To provide code pls describe exactly how you'd create an object A and AA to understand what's common and what differs.

as.
0
Tal TeneAuthor Commented:
host.javaguest.java

Notice the UserCredentials class.
It contains a reference to a different object - UserNamePassword and FirstLastNameEmail. And this is all the difference between the two classes.

Thank you tor your time, although I begin to think really about un-nesting the classes.
But then again - I cannot change the classes names because they represent JSONs. So I am not sure that would be helfull...
0
SurranoSystem EngineerCommented:
Well you still haven't revealed how you plan to instantiate the two classes (identical constructors? or different ones?)

Anyway, now it's easier to understand.

If you have to stick with the class names then you can (and should!) un-nest the identical classes and keep the UserCredentials classes nested deep.

I don't have experience with JSON annotation but there are nice tutorials that point you to the proper direction.

The following link is not about annotations but shows you how json classes should be implemented:
http://www.tutorialspoint.com/json/json_java_example.htm

The following uses XML annotations:
http://stackoverflow.com/questions/11001458/json-java-object-to-json

And there are many more.

So instantiating is, likely, you invoke the deserializer of your choice with a JSON string... or create the object manually, whichever you like.
If you need some behaviour that works both with host (having UserNamePassword) and guest (having FirstLastNameEmail) then you have to interface them. Something like:

public interface UCInterface {
  void setCred(String s1, String s2, String s3);
  Object getCred();
}

public class UserCredentials implements UCInterface {
  ...

  public void setCred(String s1, String s2, String s3) {
    UserNamePassword.UserName = s1;
    UserNamePassword.Password = s1;
    UserNamePassword.PasswordEncryptionType = Integer.parseInt(s3);
  }
  public Object getCred() {
    return UserNamePassword;
  }
}

Open in new window


Similar for Guest (but you don't even need parseInt here)
Note that I haven't tested, not even compiled the code above so it may bleed for several wounds but may give you an idea...

Thereafter, whenever you want to manipulate that object, be it host or guest, you can invoke setCred and getCred, accordingly.

Closing note: a public class should always be in a file called <ClassName>.java ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.