aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overload and override method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overload and override method question " Watch "overload and override method question " New topic
Author

overload and override method question

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
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

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
hi alvin

nicholas is right, this feature is called "late binding" in java


sandy
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

And is also a great piece of knowledge to know and understand.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Dec 31, 2004
Posts: 961
Precisely this Saturday I posted a code snippet regarding that. Visit the post named Tricky Question:

Tricky Questio
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jan 08, 2004
Posts: 834
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jan 19, 2005
Posts: 7
(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

Joined: Aug 27, 2004
Posts: 411
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...


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
thank you everyone here , i'm now clear about it
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might want to read this as well...

http://www.coderanch.com/t/375656/java/java/Method-calling


"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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overload and override method question