简体   繁体   中英

Superclass references to subclass objects

I am confused: I have taken the following quotes (with the titles of the sections in which they appear) from a learning resource, but the quotes seem to me to contradict each other.

Superclass References and Subclass Objects

"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"

Overridden Methods Support Polymorphism

" it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed"

Any clarification on this would be appreciated.

If classB extends ClassA and you have :

ClassA a = new ClassB ();

Using the a variable, you can only access members defined in ClassA (or super-classes of ClassA or interfaces that ClassA implements). You can't access a member defined in ClassB that's not defined in ClassA (unless you cast a to ClassB ).

However, calling a method of ClassA that's overridden in ClassB will execute the ClassB method.

Superclass References and Subclass Objects

Assuming Child extends Parent, let's look at this:

Parent obj = new Child();

Now if we try to use obj , we can only use behavior (methods) specified in the Parent class. We can't use any new methods from the Child class.

However, let's say both Parent and Child have a method public String whoAmI() .

Parent:

 return "Parent";

Child:

return "Child";

Now if we run this code:

Parent obj1 = new Child();
Parent obj2 = new Parent();

System.out.println(obj1.whoAmI());
System.out.println(obj2.whoAmI());

Output:

Child
Parent

So you can only access methods in the class you're referring to it by (Parent in the first snippet). But if you've overriden it in the class it is instantiated as (Child in the first snippet), and override a method in the parent in the child, then invoking the method present in the parent will call the method overriden in the child.

Have a look at below program.

class SuperMem{
    int i = 90;
    int j = 100;

    void show(){
        System.out.println("parent show..");
        System.out.println("Show inside Parent start");
        System.out.println(i + " " + j);
        System.out.println("Show inside Parent END");
    }
}

class submem extends SuperMem{
    int i = 10;
    int j = 20;
    void show(){
        System.out.println("Child show ..");
        System.out.println("Show inside Child start");
        System.out.println(i + " " + j);
        System.out.println("Show inside Child END");

    }
}

public class SuperMemDemo {
    public static void main(String[] args) {
        SuperMem m = new SuperMem();
        submem s = new submem();
        m = s;

        System.out.println("i " + m.i);
        System.out.println("j " + m.j);
        m.show();
    }

}

Output:

   i 90
   j 100
   Child show ..
   Show inside Child start
   10 20
   Show inside Child END

Methods are resolved via dynamic dispatch methods ie at runtime. Analyse output and you will get meaning of above two statements from Complete reference Herbert Schildt

Suppose we have two classes.

class Vehicle{
    
    public void drive(){
        System.out.println("Vehicle is Moving");
    }
}
class Car extends Vehicle{
    
    public void drive(){
        System.out.println("Car is Moving");
    }
    
    public void playMusic(){
        System.out.println("Car is Playing Music");
    }
}

"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"

It means if we have a code like Vehicle vehicle = new Car(); Using vehicle object, we can call drive() , but not playMusic() , Because type of vehicle is Vehicle .

"it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed"

It means if we have a code like

Vehicle vehicle = new Car();
vehicle.drive();

It will print "Car is Moving" not "Vehicle is Moving", becasue the object stored in vehicle is an instance of Car .

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