Hi, I'm
testing myself on some concepts and came across this problem:
public class Parent {
private void print() {
System.out.println("In Parent");
}
public static void main(
String[] args) {
Parent p1 = new Parent();
p1.print();
p1 = new Child();
p1.print();
Child c1 = new Child();
c1.print();
}
}
class Child extends Parent {
public void print() {
System.out.println("In Child");
}
}
When I run it, the output is:
In Parent
In Parent
In Child
Why does the second p1.print() give "In Parent" instead of "In Child"? Does this mean the print() method in Child class is shadowing the parent's print() like with varibles?