here ex is parent refernce of the child object
if I declare int x in derived class, the output changes to 20 , 20
however, if i dont declare x in derived class , the output is 30 , 4
i am thinking if i declare x in derived class , do 2 copies of x gets created?
please help
class base
{
public base()
{
x = 20;
}
int x = 2;
public void setval()
{
x = 5;
}
}
class derived extends base
{
int y = -1;
public derived()
{
x = 30;
}
int x = 3;
public void setval()
{
x = 4;
}
}
public class Inheritance {
public static void main(String[] args) {
base ex = new derived();
System.out.println(ex.x);
ex.setval();
System.out.println(ex.x);
}
}