Home arrow static arrow Java Programming [Archive] - quick polymorphism question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - quick polymorphism question
This topic has 3 replies on 1 page.

Posts:211
Registered: 5/12/03
quick polymorphism question  
Jun 22, 2004 8:11 AM



 
Hi all

I need people to either confirm or deny the following claim.

I have two classes A and B where B extends A.

In my program I have a create method with returns either A or B and it is used

A o1 = create();

Since B isA A I can return either B or A from create. The question is:

say B has some methods that A does not have. Later in the program I cannot invoke any of those methods on o1, as it is only of type A. I could use a cast, however, to overcome this problem...

Thanx.
 

Posts:13,769
Registered: 00-11-29
Re: quick polymorphism question  
Jun 22, 2004 8:14 AM (reply 1 of 3)



 
say B has some methods that A does not have. Later in
the program I cannot invoke any of those methods on
o1, as it is only of type A. I could use a cast,
however, to overcome this problem...

This isn't a question. What's the question? Are you asking if that is correct? Yes. It's correct.
 

Posts:3,258
Registered: 00-08-28
Re: quick polymorphism question  
Jun 22, 2004 8:20 AM (reply 2 of 3)



 
Later in
the program I cannot invoke any of those methods on
o1, as it is only of type A.

Yes you are right in there.

I could use a cast,
however, to overcome this problem...

Thanx.
 

Posts:211
Registered: 5/12/03
Re: quick polymorphism question  
Jun 22, 2004 8:23 AM (reply 3 of 3)



 
Sorry forgot the ?. But my question was is that correct, and you confirmed my thoughts. Thanx.
 
This topic has 3 replies on 1 page.