I am trying to understand what I am doing wrong with this code but I don't find the answer. Why it does not print anything in the compiler?
Please help. Thanks in advance.
public class ClassA {
int x = 0;
int y = 1;
public void methodA() {
System.out.println( "x is " + x + " , and y is " + y) ;
System.out.println( "I am an instance of: " + this.getClass().getName() ) ;
}
}
class ClassB extends ClassA {
int z = 3;
public static void main(String[] args) {
ClassB obj1 = new ClassB();
obj1.methodA();
}
}
Because you are only compiling , not running the code. Run it. It will print the result to console for sure.
No Overriding concept there in your code as of now. Every Children is Parent. You'll have access to that method and it prints the implementation of Parent (ClassA).
Coming to the concept of Overriding, if you want to see the implementation of methodA() in ClassB, then you need to Override it in ClassB and provide implementation related to ClassB.
I don't have any problems, everything works as it should. It prints:
x is 0 , and y is 1
I am an instance of: test.ClassB
Process finished with exit code 0
Check your IDE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With