1
class a{
    void display(){
        System.out.println("display1");
    }
}

class b extends a{
    void display(){
        System.out.println("display1b");
    }
}

class c extends b{
    void display(){
        System.out.println("display1c");
    }
}

class ab {
    public static void main(String args[]){
        a mn=new c();
        mn.display();
    }
}

it's example of late binding,but when i compile this code and then decompile using cavaj i found this code ab.java:

class ab {
    ab(){}

    public static void main(String args[]){
        c c1 = new c();
        c1.display();
    }
}

If it's late binding then compiler not must should know that display() method is from c class , but here it's know because it replace
a mn=new c(); with
c c1 = new c(); ,so this is early binding??

Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
Dhaval
  • 389
  • 1
  • 10
  • 4
    Please format your code and respect naming conventions. – Alexis C. Nov 16 '13 at 10:04
  • 2
    You are using a local variable so the compiler knows its scope and can perform these optimizations, which don't affect the behavior of the code. Perform the same operations making `c1` an attribute and check what happens then. – SJuan76 Nov 16 '13 at 10:05
  • @SJuan76 and how I make c1 an attribute? – Dhaval Nov 16 '13 at 10:31
  • you override the display. so no need to refer class a display method. because c is a child of a. – subash Nov 16 '13 at 10:41
  • Define `mn` outside the `main` method. Do not make it private. Anyway, if you don't know what an attribute is, probably you should first concentrate in learning the basis of the language before exploring its guts. – SJuan76 Nov 16 '13 at 11:27
  • @SJuan76 got the attribute but still confused,this happens because main method is static ?? – Dhaval Nov 16 '13 at 11:57

0 Answers0