wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Polymorphism Concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism Concepts" Watch "Polymorphism Concepts" New topic
Author

Polymorphism Concepts

robert stevens
Greenhorn

Joined: Jul 05, 2007
Posts: 7
If class A has funA()
If class B extends A & has additional funB()
If class C extends A & has additional funB()

If i wanna have an array which stores objects of both class A and B
how do i do it....(assuming that i may wanna access funcB())
robert stevens
Greenhorn

Joined: Jul 05, 2007
Posts: 7
sry for the typin mistake above.....
i want to store an array which holds objects of both class B and C
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

An instance of B IS-AN A, and an instance of C IS-AN A. So if the array holds elements of type A, there should be no problem in adding instances of B or C.

The trick is in using the array reference to access any methods defined in the subclass (for example, funB()). To do that, you will need to downcast the reference from type A to type B or C. But before you attempt the downcast, you should take some precautions to verify the true runtime type (B or C) -- otherwise, you will get a ClassCastException if the type is wrong.

Have you tried writing any code to experiment with this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you are certain you'll only deal with B & C ... create an interface HasFunB that defines funB() and make B and C implement that interface. Then you can make your array hold only HasFunB:

You won't even be able to put an instance of A in the array.

So, a) does that make sense and b) are you able to restrict what goes into the array that well?

If there's any chance an A could get into the array, go with Marc's suggestion for sure.
[ July 05, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stan James:
If you are certain you'll only deal with B & C ... create an interface HasFunB that defines funB() and make B and C implement that interface. Then you can make your array hold only HasFunB...

The power of interfaces -- nice and clean!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism Concepts
 
Similar Threads
Curious case about inheritance & Interface
Overloading or Overriding?
calling function from another function
Passivation of SB while in transaction
jsp:useBean "type" attribute!!!