|
Re: Polymorphism Questions
|
Posted: Mar 7, 2004 7:04 PM
|
|
> > > > BaseClass bc = new DerivedClass();
> >
> > > > So, is a fair way to describe the above statement that > "bc > > is a DerivedClass object with a BaseClass interface but > > can be downcasted"? > > > No. bc is a DerivedClass object. But DerivedClass > objects are also BaseClass objects is much the same way as > Cats are Mammals. You are dealing with inheritance, not > interfaces here. (Although I can see why you'd mix the > two up.)
I see what you're saying, and you're correct - but the term "interface" may be used more generically than the java interface type. When the bc variable is declared to be of type BaseClass, its contract is to adhere to the BaseClass interface, or the set of method signatures for the BaseClass.
Although we know that this bc is actually a DerivedClass instance, you can't invoke any DerivedClass methods that are not also declared in BaseClass without first casting it to DerivedClass and thereby not invoking the method on dc, but on the cast or a new variable declared as a DerivedClass. Unfortunately there are no exclusively DerivedClass methods in this example.
In summary, I believe it is correct to say, as Kevin asked, "bc is a DerivedClass object with a BaseClass interface but can be downcasted" because the term interface is clearly used as a description of the set of defined BaseClass method signatures, rather than confusing the concrete BaseClass with a literal java interface.
|
|