I have 2 classes in JAVA:
Parent Class:
public class Parent {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge(){
return this.age;
}
}
Child Class:
public class Child extends Parent{
public static void main(String[] args){
Parent p = new Parent();
p.setAge(35);
System.out.println("Parent "+p.getAge());
Child c = new Child();
System.out.println("Child " + c.getAge());
}
}
Output is:
Parent 35
Child 0
The private members are not inherited in JAVA
When calling getAge()
method on child class instance, why does it run successfully and even gives output as 0
?