dcsimg
Method Accepting Subclass
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Steve_Johnson
Posted On:   Thursday, October 18, 2001 06:42 AM

I'm fairly new to Java/OOP and have a quick question. Lets say I have an abstract class People . I have soemthing like 10 classes that extend People like Students , Professionals , etc. Now I have a function that accepts an instance of tpye Person . How can I create a new instance of the same type passed inside of this function? I can't say Person myPerson = new Person(); because it is abstract and I want a person of the same subclass (ie, Students ). Is there anyway to do this without doing a conditional w/ instanceof w/ each subclass? This is really not scalable, which is going to be a requirement... Any ideas?    More>>

I'm fairly new to Java/OOP and have a quick question. Lets say I have an abstract class People . I have soemthing like 10 classes that extend People like Students , Professionals , etc.



Now I have a function that accepts an instance of tpye Person . How can I create a new instance of the same type passed inside of this function? I can't say Person myPerson = new Person(); because it is abstract and I want a person of the same subclass (ie, Students ).



Is there anyway to do this without doing a conditional w/ instanceof w/ each subclass? This is really not scalable, which is going to be a requirement...



Any ideas?



Thanks,
Steve

   <<Less

Re: Method Accepting Subclass

Posted By:   Anonymous  
Posted On:   Thursday, October 18, 2001 06:58 AM

Person myPerson = new Student();
About | Sitemap | Contact