I have a class called Parent which has a private variable and a method named display to display that variable value. In a class called Child , parent is inherited.
public class GoodDay {
public static void main(String[] args) {
new Child().display();
}
}
class Parent
{
private int i=5;
public void display()
{
System.out.println(this.i +" "+this.getClass());
}
}
class Child extends Parent
{ }
Output 5 class p1.Child
1) this.getClass()
gives p1.Child
as the value. So this display method is a inherited method copy which is available in the Child class. Then how can it access the Parent class private variable without using public getter and setter?
2) Why this keyword in this.i
doesn't give any error as there is no i in child class?
I checked this link and it says that even the private variables are available in the child class objects and not in child class. How and Why ?