chhaya
chhaya

Reputation: 167

Confusion in Java polymorphism

I am having confusion in java polymorphism. In dynamic method binding jvm decides at run time which class method has to call. Suppose I am having three classes A, B and C.

class A{
    int get(){
        return 10;
    }

    int getParent(){
        return 10;
    }
}

class B extends A
{
    int get(){
        return 20;
    }
}

public class C
{
    public static void main(String args[])
    {
        A a = new A();
        A a1 = new B();
        System.out.println(a.get());/////////////////////////LINE1    
        System.out.println(a1.get  ());////////////////////////LINE2    
        System.out.println(a.getParent());////////////////////////LINE3
    }
}

I am having confusion in line 1 and line3 at compile time and runtime binding. In line 3 it a.getParent() and this method is in parent class only so what it has to decide at runtime.

In line 1 both reference and object are from same class so again what it has to decide .

Please send me any good link for runtime and compile time binding how works.

Upvotes: 0

Views: 317

Answers (4)

codeMan
codeMan

Reputation: 5758

polymorphism here applies only in the case of line2. There is no concept of polymorphism applied to line1 and line3.

Upvotes: 0

Srinivas B
Srinivas B

Reputation: 1852

    class A
    {
      public doIt( )
      {
        //this does something
      }
    }

    class B extends A
    {
      public doIt( )
      {
        //this does something
      }
    }

    class C extends B
    {
      public doIt( )
      {
        //this does something
      }

    }        

    public static void main(String[] args) {
          A x = new B( );

          x.doIt( );

    }

The statement that causes a lot of confusion is the “A x = new B();” statement. Although the variable x is an object of type A, it is instantiated as an object of class B – because of the “= new B( );” part of the statement. The Java runtime will basically look at this statement and say “even though x is clearly declared as type A, it is instantiated as an object of class B, so I will run the version of the doIt() method that is defined in class B.”

The version of the doIt() method that’s executed by the object x is the one in class B because of what is known as dynamic binding in Java – the code above can be considered to be an example of dynamic binding. Dynamic binding basically means that the method implementation that is actually called is determined at run-time, and not at compile-time. And that’s why it’s called dynamic binding – because the method that will be run is chosen at run time. Dynamic binding is also known as late binding.

In early binding the data and method are binds at the complie time where as in late binding the data and method will bind at the runtime.

Upvotes: 3

Joop Eggen
Joop Eggen

Reputation: 109613

Class A provides the Object instance a with a virtual method table, containing A.get and A.getParent.

Class B provides the Object instance a1 with a virtual method table, being first taken from class A, and expanded (here nothing to expand with). The get method is overwritten with B.get.

a1.get, even being a A, will call B.get.

Upvotes: 0

cowls
cowls

Reputation: 24354

Class B overrides the get() method. So whenever you call get() on an object that is of type B it will use the overridden method.

Because B doesnt override getparent(), then the parent getParent() will be called when you call it on class B

Upvotes: 0

Related Questions