File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to create a new object with given superclass reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create a new object with given superclass reference?" Watch "How to create a new object with given superclass reference?" New topic
Author

How to create a new object with given superclass reference?

Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47



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....


Live Curious!!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

I changed your "quote" tags into "code" tags to improve readability.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 15, 2011
Posts: 47

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 15, 2011
Posts: 47

Thank you so much..... though Simplicity has been killed.......this is what i need..

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


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 15, 2011
Posts: 47

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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a new object with given superclass reference?