Why the output of this program is:
person
person
I know that the inheritance means that the subclass take the properties of the superclass, here is the code:
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return "Student";
}
}
class Person {
private String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
I know that getinfo()
method is not inherited and not overridden because it is private
. But, there is another method in Student
class that has the same name and returns Student
.
I want to know why that is happening? How JVM works with inheritance exactly?