• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overload and override method question

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good day , i can't figure out why "Animal ah = new Horse(); " resulting
"horse eat glass"

Animal class


Horse class

handler class


result :



can expert here kindly please explain why the output


not using Animal reference to call eat method but rather using Horse object to call method , when would it call method by using reference variable rather than object instance ? and also the in contrast ..thank you
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because the feature of dynamic binding. In runtime, the Animal object in fact is a Horse, and thus, the method declared in Horse will be invoked. Noticed that the methods are resolved in runtime. However, variables are resolved in compilation time.

Nick
 
shandilya popuru
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi alvin

nicholas is right, this feature is called "late binding" in java
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And is also a great piece of knowledge to know and understand.

Mark
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's basic principle of polymorphism. Any animal object can be a horse. So the animal you created is actually a horse, not a simple animal. When you call the eat method, you are calling the method on a horse instance accessed through an animal interface (by interface I mean the exposed methods of Animal).

That way you could cast to animal object any kind of specialized animal derived from it and the code you specialize in every single subclass is polymorphically executed, that's to say, animal will assume different forms depending on the real reference it containts at runtime.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for reply , but i still can't figure out why sometimes(don't have any sample here) it will run the method from base class rather subclass? by using the reference variable ..

can someone provide sample of it ? thank you
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I guess u r confused by the overriding of instance methods and static methods.
1) static methods don�t participate in overriding, since they are resolved at compile time based on the type of reference variable.

2) Member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since it�s already resolved at compile time based on the reference variable type. Only methods are resolved at run-time


u can also go through the explanation here
http://jminds.hollosite.com/Java/funda/funda6.htm
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precisely this Saturday I posted a code snippet regarding that. Visit the post named Tricky Question:

Tricky Questio
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , as following sample code , why this time we can only call Animal method() ? but not horse's method() ?

animal class


Horse class


anyone can explain about it ? thank you
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During compile time, the method horseCall() will not be found in Animal class. Even though the method horseCall of Horse class will be finally called, but for the program to compile, horseCall() method should be present in Animal class.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i confuse here , since sometimes it seem using horse object(first sample code) to call method , sometime it use Animal reference(above previous code) to call method ..
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See dude,
Which method has to be called is decided during run time and not compile time. During compile time, the compiler only see the reference variable which it is associated to.

So according to ur example, the reference is of Animal. So, it will search based on the Animal Class and since horseCall() is not present in the Animal class, it displays an error there.

Is this clear enough?
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but as refer to my very first post sample code as follow :



why it call horse's eat ? but not Animal's eat() ?
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i have already said:
Which method has to be called is decided during run time and not compile time. During compile time, the compiler only sees the reference variable which it is associated to.


The JVM insures that the method called will be from the real class of the object (not with the variable type declared). This is accomplished by virtual method invocation (late binding). This is done only during run time.
So, during compilation, since the reference is of Animal type, the method horseCall() is searched in Animal class and as its not found, error is displayed.
 
You Lu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(regarding the second code)
At compile time:

since: Animal a = new Horse();
hence: The reference variable type is Animal
&
since: horseCall() is not located in the Animal class
hence: a.horseCall() couldn't found, error occurs

in order to make it correct, you need to make sure the compiler could find the method u called which is inside the class corresponding the reference variable type.

(regarding the first code)

it's late binding, it's regard to the actural object.
-------------------------------------------------------------------------
Which method has to be called is decided during run time and not compile time. During compile time, the compiler only sees the reference variable which it is associated to.

The JVM insures that the method called will be from the real class of the object (not with the variable type declared). This is accomplished by virtual method invocation (late binding). This is done only during run time.
So, during compilation, since the reference is of Animal type, the method horseCall() is searched in Animal class and as its not found, error is displayed
---------------------------------------------------------------------------
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alvin,
Here are my inputs on this topic . Hope I am not confusing more.







Follow this steps to find out which method will be called

Let us analyze line 1
a.eat();

1> Here ask yourself question ---> Is eat() method overridden ?
Ans-> Yes !!! .

2> Now look at the object reference "a" and ask question "What are the object contents" ?
Ans-> They are of Animal. So eat() method of Animal will be called.

Let us analyze line 2
h.eat();

1> Here ask yourself question ---> Is eat() method overridden ?
Ans-> Yes !!! .

Again,

2> Now look at the object reference "a" and ask question "What are the object contents" ?
Ans-> They are of Horse. So eat() method of Horse will be called.

Let us analyze line 3
ah.eat();


1> Here ask yourself question ---> Is eat() method overridden ?
Ans-> Yes !!! .

Again,
2> Now look at the object reference "a" and ask question "What are the object contents" ?
Ans-> They are of Horse. So eat() method of Horse will be called.


Note in this case : The contents of object reference "ah" are of Horse but the type is of Animal.




Basic rule if the method called is overridden contents of the object decide which method is going to be called.
if the method is not overridden, then type of the object decides which method is called.



Hope that helps you...
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you everyone here , i'm now clear about it
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to read this as well...

http://www.coderanch.com/t/375656/java/java/Method-calling
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic