• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

method overriding doubt

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Pls any one can tell me, in method overriding when it calls base class version, when it calls subclass of the method??
for example class A extends B , B is having a overriding method prn()

which class version will be called

Thanks in advance
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

madhu
----------------------------------------------------------------------
for example class A extends B , B is having a overriding method prn()
B b=new A();
b.prn();
-------------------------------------------------------------

See here the object type is A so the method which has to be executed is the
method defined in class A.

Thanks
Anil Kumar
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It calls the prn() version in the class A. It depends on the runtime object the reference variable B contains. In this case, B is referring to an object of type A. So, the method in the class A will be called.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Madhu!

Lemme try to explain. Consider the following code.

class Base {

void method() {
System.out.println("In Base class!");
}
}

class Derived extends Base {

int k=1;
public void method() {
System.out.println("In Derived class!");
}
void test(int i) {
System.out.println("Derived.i=" + i);
}

public static void main(String args[]) {
Base b = new Derived();
Derived d = new Derived();
Base b1 = new Base();
b.method(); //line1-->yields - In Derived class!
b.test(k); /*line 2-->compiler error as no such method as test(int i) in Base class*/
d.test(); //line 3-->yields - Derived.i=1
b1.method(); //line 4-->yields - In Base class!
}
}

Here you go....if we declare a reference of super type and object of subtype then the method called depends on the runtime type of the object.But first of all the compiler chks whether the method being called using a reference exists in the class(type of reference). If it does exist then compiler gives a go as in line 1,3 and 4.Then the method invoked at runtime depends on the runtime type of the object.
Hope the above explanation helps..

Ranchers, pls correct me if am wrong.

Cheers
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok 2 points from my side,

1. To summarize, which overridden method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type passed at compile time.
2. Object type determines which overridden method is used at runtime and Reference type determines which overloaded method will be used at compile time.


Hope it helped you out, if so say cheese.

Gitesh
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhu,

  • Method Overriding relies on the actual object being accessed.
  • Variable Shadowing depends on the type of the variable in which the object´┐Żs reference is stored.



  • Consider the following program:




    If you run the program, the output will be


    Base class Construtor!
    Derived class Construtor!
    b.value is : 1
    derived version
    b.getValue() : 2



    You can conclude,

  • For Methods: When you invoke a method (which is overriden of course), the runtime object with which the method is invoked is examined and the appropriate version is called!


  • Even though you call the getValue() method with repsect to the base class reference variable "b", its actually pointing to the derived class object at runtime. So, the getValue() method of derived class is what getting invoked!

  • For Variables: It means, the actual reference with which you access determines the class whose variable is gonna be accessed.

  • If You invoke the * shadowed variable * through the direct object reference and not through any method , the variable present in the base class will be invoked.

    You access the "value" variable with respect to the base class reference variable "b" directly. Even though its pointing to the derived class object at runtime, for variables, the actual runtime object is NOT checked, instead just the reference variable alone does matter!. So, the value of base class is accessed.


    Hope this clears.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thats perfect Neha and Gitesh!
     
    Ranch Hand
    Posts: 185
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To add,
    1)Lets sat there i a methos display() which is overriden by the subclass.
    If you say like
    super s = new sub();
    s.display();
    First compiler checks whether super does contain the display or not!!! if display is not visible, then the compiler will shout!!!
    Next, if super contains the display method,
    at run time the method in the sub class will be called. this is know as dynamic binding. The compiler will call the lowest method in the hirarchy.
    Hope you got the point.
     
    madhu v pe
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you all
    Thanks to Gitesh's 2 points which meets my doubt.

    Originally posted by Gitesh R:
    [QB]Ok 2 points from my side,
    1. To summarize, which overridden method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type passed at compile time.
    2. Object type determines which overridden method is used at runtime and Reference type determines which overloaded method will be used at compile time.
    /QB]



    I tried this code

    yes gitesh is right,

    when base class reference is pointing to derived class object, [this is my criteria]
    if it is overloading it calls the base class version,
    if it is overrridding it calls the derived class version.
    so the output is
    --------------------
    in base overload1
    in derived overridden
    --------------------

    Thanks
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thats fine Madhu. Great to have a conclusion arrived.

    I think you have typed the C++ version of the code by ending the class definition with a semicolon

    The Reference type is checked for shadowed variables as well.
     
    madhu v pe
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Raghavan,

    Thanks alot. yeah the base class versioned variable will be used.

    I think placing semicolon at the end of the class is legal in java.
    I am using java Editor which is creating barebone class ending with semocolon like this.

    Thanks
     
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Looks like me missed one point here.

    B b=new A();
    b.prn();

    By any chance method prn() is static then it will call prn() from base class.
     
    madhu v pe
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Himanshu ,

    [QB]Looks like me missed one point here.

    B b=new A();
    b.prn();

    By any chance method prn() is static then it will call prn() from base class./QUOTE]

    Can you elaborate what do you want to say?
    making static prn() in derived class?
    by the by static method cannot be overridden.

    Thanks

     
    reply
      Bookmark Topic Watch Topic
    • New Topic