Aditya Singh
Aditya Singh

Reputation: 678

Accessing child properties from parent type reference in java

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 ?
 }

Upvotes: 2

Views: 7086

Answers (4)

S G
S G

Reputation: 51

Here is all possible combination There is difference at runtime while access member variable and member method

class Parent {
    String s = "parent class variable ";
    public void display()
    {
        System.out.println("Parent class method");
    }
     
}
class Child extends Parent{
    String s = "child class variable ";

    public void display()
    {
        System.out.println("Child class method");
    }
    
    public static void main(String[] args) {
        Parent p = new Parent();
        Parent c = new Child();
        Child cc = new Child();

        System.out.println(p.s);
        System.out.println(c.s);
        System.out.println(cc.s);

        p.display();
        c.display();
        cc.display();


    }
}

Output :

parent class variable
parent class variable
child class variable
Parent class method
Child class method
Child class method

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 678

Here when you write Parent p = new Child(), then object of child class is created with properties of both Parent and Child class but the reference variable which is used to hold this object is of Parent type or you can say of Parent class.

When we want to access the instance methods or variables of any class, then the reference variable should be of that class only or its child class.

So we cannot access variable of child class with the reference variable of parent class. Only the variables present in the Parent class can be accessed through the reference variable of parent class, doesn't matter you are using that reference variable to hold parent Class object or Child class object.

So only way to access 'a' instance variable in the code from p reference variable is to typecast p to Child type and then it can access variables of Child class.

`
Parent p = new Child();
  System.out.println(((Child)p).a);

`

Upvotes: 0

Khushit Shah
Khushit Shah

Reputation: 555

You can do it by typecasting the reference to the child type.

class Parent {
}

class Child extends Parent {
    int a = 20;
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(((Child)p).a);
    }
}

It will throw a ClassCastException if p is not an Object of Child Type. So, it is better to check that p is an Object of Child by instanceof operator

    if (p instancof Child) {
        System.out.println(((Child)p).a);
    }

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23503

The reason it isn't working is because parent classes don't have access to their childrens properties. You may be creating a Child class, but you assign it to a Parent object. Since Child is inheriting from Parent you are able to cast Child to Parent.

You are basically doing:

 Parent p = (Parent) new Child();

In other words, You are creating a Parent object. There is no a attribue in the Parent class.

You can do:

class Parent{
    int a = 20;
}

class Child extends Parent{

public static void main(String[] args){
     Child c = new Child();
     System.out.println(c.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 ?
}

Upvotes: 0

Related Questions