-1
class A{

int x= 30;
void printA(){
Sysout(x);
}
}

class B extends A{
int x= 40;
}

class MyMain(){
public static void main(String args[]){

B obj = new B();
obj.printA(){
}
}

B is the child class of A. Why the output is coming t be 30?? It should b 40? Object of B should have value of x as 40. And sysout(x) means this.x

Payal Bansal
  • 725
  • 5
  • 17

2 Answers2

1

You are calling the print that refers to A.x and not to B.x. You should try to override printA in B

0

Even though you are calling the method using B, it is executing method from A as it have access. And you are using the variable from class A in that method.

And sysout(x) means this.x

Yes, in your case, this means A as you not overridden that method.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307