简体   繁体   中英

Overriding with Superclass Reference for Subclass Object

I have recently been moving through a couple of books in order to teach myself Java and have, fortunately, mostly due to luck, encountered very few difficulties. That has just changed.

I read a section on the following under inheritance and the whole superclass subclass setup

-- When a new superclass object is created, it is, like all objects, assigned a reference (superReference in this example)

-- If a new subclass object (with the defining subclass extending the superclass) is created, and then the superReference reference is set to refer to that instead of the original object, it is my understanding that, since the reference is made for a superclass, only members defined by the superclass may be accessed from the subclass.

First - is this correct?

Second: If I am overriding a method and therefore have one in the super and one in the sub, and I create a superclass object and then assign its reference, as I did above, to a subclass object, by the principle called something like Dynamic Method Dispatch, a called overridden method should default to accessing the subclass method right?

Well, my question is:

If a reference to a superclass-object is retooled for a subclass-object and will deny direct object.member access to subclass-defined members, only supporting superclass-defined members, how can, if a superclass reference is retooled for a subclass object, an overridden method apply to the subclass-object if access is limited by the superclass-originated-reference-

If you try like:

class SuperClass{
    int intVar = 0;
    void method(){
        //in super class
    }
}
class SubClass extends SuperClass{
    int intVar = 2;
    void method(){
        //in sub class
    }
}

Then

SuperClass obj = new SubClass();
int val = obj.intVar;// this is taken from SuperClass as variables are decided on reference basis
//if both superclass and subclass contain the same variable it is called shadowing
obj.method();// it is taken from the SubClass as it is method overriding 
//and is decided at runtime based on the object not the reference  

Check comments. Hope this helps.

only members defined by the superclass may be accessed from the subclass.

First : This is just plain wrong. The subclass may access it's own member without a problem. However once you have assigned a subclass instance to a super class variable (reference) the you can only call methods or members made accessible from the super class only. Is this what you meant to say?

Second : Methods that will be executed are the methods in the instance (object). Not the methods in reference (variable) type. So yes overridden methods will always be executed.

Third : A subclass may override a method but not a instance property. Whatever member variables are in the super class will be in the subclass as well. And you can override methods in the subclass just so long as you keep their existing access modifier or use a more accessible access modifier.

In Oracle documentation doesn't mention that or at least it is not clear or explicitly explained. This behaviour seems to me like virtual methods in C++ with the difference that in such language it is clear through the use of the keyword preceding the methods defined in the base or parent class (superclass in java) and that must be redefined in the child class. 来清除,并且必须在子类中重新定义类。 In C++ there are not virtual variables, just virtual methods.

In that case, when we have a pointer (reference) to a base class that points to an instance of a child class and there are methods with the same signature and variables with the same name in both the parent and child classes, the definitions of the methods that will be executed are those of the parent class if they are not preceded by the keyword , on the other hand the definitions in the child class will be executed for the methods declared as . 话,将要执行的方法是父类的方法,另一方面,将对声明为的方法执行子类中的定义。

In the case of the variables, the ones in the base class are taken and not the ones in the child classes.

Resuming, the similarities are:

-In java, variables are taken based in the reference -In C++ there are not virtual variables

So in the case we are talking about, hidding variables in the child classes or subclasses are not taken but the hidden

-In java, methods are taken based on the object or instance -In C++ virtual methods must be redefined in the child classes and those are taken

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