0

While trying to hands on the code I did not understand this portion

public class parent {
    int a =5;
    parent(){
        this.a =7;
    }

    void print() {
        System.out.println("In parent");
        System.out.println(this.a);
    }

}

public class child extends parent{
    int a =8;
    child(){
    }
    void print() {
        System.out.println("In child" + this.getClass().getName());      
        System.out.println(this.a);
    } 
    public static void main(String args[]) {
        parent childobj = new child();
        childobj.print();                      
        System.out.println(childobj.a);
    }
}

While childobj.print() print 8 why childobj.a will print 7 as this refers the same object?

khelwood
  • 55,782
  • 14
  • 81
  • 108
abhishek
  • 149
  • 12

0 Answers0