0

On running below code i am getting this answer Parent 50 Child 50 Parent 50

But I believe it should return Child 90. I am confused over this:

public class PolySameVar {
    public static void main(String []args)
    {
      Parent par = new Parent();
      par.getx();
      Child chld=new Child();
      chld.getx();
      Parent parchld= new Parent();
      parchld.getx();
    }
}


class Parent {
    private int x;
    Parent(){
        x=50;
    }
public void getx(){
        String className = this.getClass().getName();
        System.out.println(className+" "+this.x);
    }
}
class Child extends Parent{
    private int x;
    Child(){
        x=90;
    }
}
Rituraj
  • 1
  • 1

0 Answers0