Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a new object with given superclass reference?

 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please help,,,

In this code, CommonList is the super class and Groups,Subscribers,News are the subclasses.......
Is there any way to make a new object of the subclass which is passed as argument to this function. My code is doing it the way i want but i think
there must be another way of doing this instead of using "if else"

Thanks....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed your "quote" tags into "code" tags to improve readability.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand my problem. Is this what you're trying to do?

You have a parent class, P, and some child classes, C1, C2, etc.

You have a method, create(P original), that takes a P as an arg, so what it actually receives could be a P or a C1 or a C2. Whatever type is passed in, you want to create a new instance of that same class.

Is this it?

If so, then you're on the right track, but you need for each subclass to have a c'tor that takes an arg that is either of type P or of the same type as the subclass, depending on if what the new object needs from the original is only stuff from the parent class or if it's specific to the subclass being passed int.


 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

ya my code is right but i just want to know..
....Just want to save the typing overhead if there are many subclasses...
....
Is there any function in the java API like

Class type = get_running_class(super_class_object);

and with this type .. i can create another subclass object ......

type ob =new type();


So that i don't have to check for the every subclass object using

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Dabas wrote:
....Just want to save the typing overhead if there are many subclasses...
....
Is there any function in the java API like
...
So that i don't have to check for the every subclass object using



Oops. I guess I didn't read your original post closely enough. Yes, you can do something like that using reflection. Here's my example with reflection instead. I've left out exception handling, just to keep it brief, and you may have to fiddle with the generics a bit, and you'll have to do some studying to learn the details, but this should get you started:


Or you can just get rid of the generics and deal with casting, if you're not familiar with that concept.
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much..... though Simplicity has been killed.......this is what i need..

...and Reflections ..I have to see this


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Dabas wrote:Thank you so much..... though Simplicity has been killed.......this is what i need..

...and Reflections ..I have to see this




In some ways this is simpler than the if/else approach. In particular, there's one block of code that handles all child types, and as you add child types later, you don't need to change this code at all.
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Sagar Dabas wrote:Thank you so much..... though Simplicity has been killed.......this is what i need..

...and Reflections ..I have to see this




In some ways this is simpler than the if/else approach. In particular, there's one block of code that handles all child types, and as you add child types later, you don't need to change this code at all.


Yes.... +1 ..... exactly the same i was looking for..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic