...with polymorphism, the reference and the object can be different. (e.g. Animal myDog = new Dog();
recently. it was actually Rob Prime, on this thread."any object has two different types.
The first one is the declared type . . .
The second type is the actual type. "
"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
Originally posted by marc weber:
When we say...
Animal myDog = new Dog();
...the reference to the Dog object is implicitly upcast to the supertype Animal. This is an example of assignment conversion (in this case, a widening reference conversion).
"In simple terms, polymorphism lets you treat derived class members just like their parent class's members." (Ref: Wikipedia: Polymorphism.) In this case, we are treating an instance of Dog as the parent type, Animal.
So with this upcasting, I think we do have polymorphism here. But we're not going to see polymorphic behavior until we call a method using this reference. For example, when we call the makeNoise() method on this Animal reference, we get Dog's implementation of the method...
Originally posted by Joey Chen:
...In the book, it mentions a superclass "Object" and a subclass "Snowboard." It states that "...even if the object is of type Snowboard, an Object reference to the Snowboard object can't see the Snowboard-specific methods...
"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
Originally posted by Joey Chen:
In the book, it mentions a superclass "Object" and a subclass "Snowboard." It states that "...even if the object is of type Snowboard, an Object reference to the Snowboard object can't see the Snowboard-specific methods.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Campbell Ritchie:
Somebody said, recently. it was actually Rob Prime, on this thread.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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
Originally posted by Ilja Preuss:
That is supposed to mean that you can't call methods that are *only* implemented in the Snowboard class - methods that don't override methods in Object.
Originally posted by Stan James:
An object only has one type
I thought I got beat up for saying that once. Must have been in a different context.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Joey Chen:
....My question is, do the same rules still apply even if I have a subclass implement an interface - I still shouldn't call a method that isn't implemented in a superclass?
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Originally posted by Joey Chen:
..In the book, it mentions a superclass "Object" and a subclass "Snowboard." It states that "...even if the object is of type Snowboard, an Object reference to the Snowboard object can't see the Snowboard-specific methods.
However, you just provided an example just like this and called the Dog's makeNoise() method.
Am I misinterpreting the book?
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
a wee bit from the empire
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|