When I create a child object with parent reference like this Parent p = new Child();
then basically it is a child object with parent reference and with properties of both parent and child.
Then if it is a child object with parent type reference then why I cannot access child properties with it.
I am trying to do the following thing:
class Parent {
}
class Child extends Parent {
int a = 20;
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.a); //gives compile time error
// question is , p is parent type reference variable , but it is pointing to object of child
// class, then we should be able to access child properties from it, but we cant, why ?
}