简体   繁体   中英

Super class reference to sub class in java

I was trying to reference child class object using parent class. According to Complete Reference Java, parent class can refer to the child class but it can access only those fields that is already declared in parent class (complete reference java 8th edition, page no:166, second last paragraph).

According to complete reference
It is important to understand that it is the type of the reference variable—not the type of the object that it refers to—that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why plainbox can't access weight even when it refers to a BoxWeight object. If you think about it, this makes sense, because the superclass has no knowledge of what a subclass adds to it. This is why the last line of code in the preceding fragment is commented out. It is not possible for a Box reference to access the weight field, because Box does not define one.

Now I am Using this example. Parent class Box.java

public class Box{
    int length;
    int breadth;
    int height;
    Box(int length, int breadth, int height){
        this.length = length;
        this.breadth = breadth;
        this.height = height;
    }
    public void getAll(){
        System.out.println("Length"+this.length+"\nBreadth"+this.breadth+"\nHeight"+this.height);
    }
}

Child Class BoxWeight.java

public class BoxWeight extends Box{
    int weight;
    BoxWeight(int length, int breadth, int height, int weight){
        super(length,breadth,height);
        this.weight = weight;
    }   
    public void getAll(){
        System.out.println("Length"+this.length+"\nBreadth"+this.breadth+"\nHeight"+this.height+"\nWeight"+this.weight);
    }

    public int getWeight(){
        return this.weight;
    }
}

Implementation Class

public class Implementation{
    public static void main(String args[]){
        Box simpleBox = new Box(10,10,23);
        BoxWeight boxWeight = new BoxWeight(10,10,10,30);
        System.out.println("box result");
        simpleBox.getAll();
        System.out.println("box weight result");
        boxWeight.getAll();
        simpleBox = new BoxWeight(10,10,10,560);
        System.out.println("Child class reference result");
        simpleBox.getAll();
        //System.out.println(simpleBox.getWeight());
    }
}

The Output is

box result
Length10
Breadth10
Height23
box weight result
Length10
Breadth10
Height10
Weight30
Child class reference result
Length10
Breadth10
Height10
Weight560

My Question is when I am referencing child class with parent object then why member variable of child class is accessible through parent class object. This should not happen according to complete reference java.

Since getAll() is a public instance method, it is "virtual": the implementation in BoxWeight overrides the implementation in Box .

You have misunderstood the passage that you are reading. The Box class does not "refer" in any way to BoxWeight or BoxWeight.weight or BoxWeight.getAll() ; rather, you're simply calling simpleBox.getAll() , and since simpleBox is an instance of BoxWeight , the relevant implementation of getAll() is the one from BoxWeight .

Where does the member variable of child class is accessed through parent class object in the above example ???

After assigning the BoxWeight object to simpleBox (the Box class reference variable), simpleBox only accessed a method getAll() . And the getAll() method called is of the class BoxWeight . It happened because, the getAll() method is there in both super class Box and subcalss BoxWeight , with same signature, which is criteria for method overriding. So the subclass (BoxWeight ) object , simpleBox overrides the getAll() method.

Thanks all for your support. Actually I got the answer, this thing is happening due to dynamic method dispatch. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM