GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help sought in Overriding? 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 "Help sought in Overriding?" Watch "Help sought in Overriding?" New topic
Author

Help sought in Overriding?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

hi guys, I need help on the topic of Overriding. I know that overriding is related to inheritance. i will state my problem using code listings. Consider following type hierarchy

Code listing 1.




I would like to know how many methods does Dog have now. Does it have 4 methods now - 2 inherited from Animal class viz. walks() and sleeps() , 1 override walks() and 1 barks() or is it have 3 methods overriden walks(), sleeps() and barks(). the confusion is whether overridden means overwriting the inherited method or is it like inherited method is still there in derived class but which one is called is decided at runtime. to state in a more simple way i would like to know that when I create Dog instance using new Dog(); will it have both the implementations of walks() method or not ? What i have assumed now is that it will have both the walks() implementation , but , which one will be called depends upon runtime type of object. Please guys help me out.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4396
    
    8

In that example Dog has three methods. The overridden method is polymorphic, which means that the version called is decided at runtime, but there's no way of calling the base-class version on a Dog object.

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

thanks Mathew . I got that Dog class has 3 methods and there is no way of calling base class walks() method because it is not there in Dog object since it has been overridden. Can you please explain what is early binding and dynamic binding in terms of above Animal-Dog example. ? I have read that at compile time compiler generates virtual calls but i'm not able to understand it . Can you please throw some light ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4396
    
    8

Dynamic binding is how polymorphism works. In the following code:
Which version of walks() gets called? The compiler doesn't know. At runtime the JVM does know, though, because it can check the actual type of the object. That's dynamic binding - the method that is called isn't fixed at compile time, it depends on the actual situation at the time the code is executed.

To do early, or compile-time, binding, the compiler would have to decide which method to call, and that couldn't then be changed by the JVM. So polymorphism wouldn't work.

If you've ever used C++ or C#: those languages can do both types of binding. The default is compile-time, and you have to specifically label a method as virtual to make it dynamic - so you don't actually get polymorphism unless you ask for it. Java doesn't bother to make the distinction.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

gurpeet singh wrote:thanks Mathew . I got that Dog class has 3 methods and there is no way of calling base class walks() method because it is not there in Dog object since it has been overridden. Can you please explain what is early binding and dynamic binding in terms of above Animal-Dog example. ? I have read that at compile time compiler generates virtual calls but i'm not able to understand it . Can you please throw some light ?



I will assume that you understand the compile time side of this... the compiler determines the method to call from the reference type (and signature used).... blah blah blah.

Anyway, for methods that may be overridden (non static methods with accessible version of it in the heirarchy), it will calls via a jump table. It means that it will not call the method directly, but instead, it will call into a jump table, and from the jump table make an extra hop to the actual method to be call. All of this is setup at compile time -- all except for the jump table, as that is part an actual instance.

At runtime, when an object is instantiated, the jump table is filled in -- the method targets will be either to the current class (if the method has been overriden), or to the correct super class (if the method is being inherited). So, this is what is meant by "dynamic binding", the actual method to use will be determined at runtime. This is also what is meant by "virtual call", the call is indirect via a jump table (or virtual table, which I believe is a term from C++).

[EDIT: beaten to the answer by less than a minute ... ]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
gurpeet singh wrote:thanks Mathew . I got that Dog class has 3 methods and there is no way of calling base class walks() method because it is not there in Dog object since it has been overridden. Can you please explain what is early binding and dynamic binding in terms of above Animal-Dog example. ? I have read that at compile time compiler generates virtual calls but i'm not able to understand it . Can you please throw some light ?


Dog class has access to all the 4 methods...You could always use the super keyword and call the base class version of walks from the walks method of sub-class. It is the Dog object which exhibits the dynamic overriding behavior.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

guys consider the following code listing :







when i ran this code it printed the output

animal walking
dog walking

In the dog's walks method i called super.walks() method which ran the superclass(overridden) version of walks(). As said by Mathew Dog object will have 3 methods. I may be wrong but dont you think Dog object will have all the 4 methods ? Just a thought came to my mind that when we did Dog d = new Dog(); superclass constructor also gets called , which created Animal object as well. and when we did super.walks() , the method actually was called on Animal object and not on Dog object. guys tell me If i'm right. I hope you got what i want to say. in short super.walks() calls method on which object and how ?
 
jQuery in Action, 2nd edition
 
subject: Help sought in Overriding?