This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
//a.bark(); Error, beacause the bark method isn't in the Animal class
Dog b = new Dog();
a=b; a.play(); a.bark(); //Error again, but why?, the "a" reference variable //refers to a Dog object of TYPE DOG
}
}
But i don't understand why when i do this:
Animal a = new Dog();
i can't refer to any methods in the Dog object using the reference variable "a" which i haven't overriden, not even instance vars.
???
Please help me people.
Thank you all.
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA
Live life to an interface, not an implementation!
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
When you write
Animal a
you are declaring that the variable 'a' will now and always refer to an Animal object. You are able to do
Animal a = new Dog();
because a Dog is an Animal. However the variable 'a' is still an Animal and therefore only Animal methods can be called on it.
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
i just deleted my post but here i am again you can allways do this:
java amateur
Alan Jump
Greenhorn
Joined: May 25, 2005
Posts: 26
posted
0
Originally posted by Steven Bell: However the variable 'a' is still an Animal and therefore only Animal methods can be called on it.
Just to make sure I've got it straight in my head, the only way to get 'a' to refer to (and therefore make use of the methods in) a Dog() class would be to break the reference first, then rereference it as a Dog(), like this:
...or would casting it at declaration time work?
I admit, polymorphism still confuses me, even with the good books and tutorials available...
If I were at my own machine I could do just that. Sadly, I'm not at a machine I can drop a JDK onto, so I have to try and process all these good ideas and suggestions in my grayware (which may be what's confusing me).
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
This is a compile time error. As I said the variable 'a' is declared as type Animal and will from then on always be of type Animal. If the Object referenced by 'a' is of type Dog the only way to call Dog specific methods on it would be
'a' can access ALL of the members in Animal (if access modifiers all it)because of the IS-A relationship, and it can ONLY access the METHODS (not instance variables) of the Dog class which override the methods inherited from the Animal class? So nothing else can be accessed in the Dog class?
Is this correct, or is there actually more to it?
Alan Jump
Greenhorn
Joined: May 25, 2005
Posts: 26
posted
0
Originally posted by Steven Bell: [QBAs I said the variable 'a' is declared as type Animal and will from then on always be of type Animal. [/QB]
*cue sound effect: light switch*
Got it now...many thanks.
Steven Bell
Ranch Hand
Joined: Dec 29, 2004
Posts: 1071
posted
0
Originally posted by Marzo O.C: So when you do this:
Animal a = new Dog();
'a' can access ALL of the members in Animal (if access modifiers all it)because of the IS-A relationship, and it can ONLY access the METHODS (not instance variables) of the Dog class which override the methods inherited from the Animal class? So nothing else can be accessed in the Dog class?
Is this correct, or is there actually more to it?
I think you pretty much have it, let me rephrase it and see if we are on the same page.
Compile Time: The compiler knows that the variable 'a' refers to an Animal Object. It doesn't know or care if the actual Object at runtime is an Animal or some subclass of Animal. Therefore during compile time you can only call things visible on the Animal class with the 'a' reference.
RunTime: (may be a bit of simplification, but the basic point) During runtime the variable 'a' is a reference (pointing to) and actual Object on the JVM heap. When 'a.someMethod()' is called the JVM looks up the actual Object that 'a' references. It looks at that Object to see if it has a method signature that matches the method being called (someMethod() in this case). If a matching method is not found it gets the superclass of that Object (when ever you create an Object all it's superclasses are also created, more or less) and looks for the method there. It will keep going up the chain until it finds the method.