简体   繁体   中英

Inheritance: Access to base class fields from a subclass

How sub class objects can reference the super class? For example:

public class ParentClass {

    public ParentClass() {}     // No-arg constructor.

    protected String strField;
    private int intField;
    private byte byteField;
} 


public class ChildClass extends ParentClass{

    // It should have the parent fields.
}

Here when the ChildClass constructor is called, an object of type ParentClass is created, right?

ChildClass inherits strField from the ParentClass object, so it ( ChildClass object) should have access to ParentClass object somehow, but how?

When you do ChildClass childClassInstance = new ChildClass() only one new object is created.

You can see the ChildClass as an object defined by:

  • fields from ChildClass + fields from ParentClass .

So the field strField is part of ChildClass and can be accessed through childClassInstance.strField

So your assumption that

when the ChildClass constructor is called, an object of type ParentClass is created

is not exactly right. The created ChildClass instance is ALSO a ParentClass instance, and it is the same object.

An instance of ChildClass does not have a ParentClass object, it is a ParentClass object. As a child class, it has access to public and protected attributes/methods in its parent class. So here ChildClass has access to strField , but not intField and byteField because they are private.

You can use it without any specific syntax.

You can access strField just as if it is declared in ChildClass . To avoid confusion you may add a super.strField meaning you are accessing the field in the parent class.

是的,您将能够从ChildClass访问strField ,而无需执行任何特殊操作(请注意,只会创建一个实例。子项将继承父项的所有属性和方法)。

here when the ChildClass constructor is called an object of type ParentClass is created, right?

No! ChildClass constructor is called >> parent class constr is called and No Object of the ParentClass is created just accessible field from the parent class are inherited in ChildClass

the ChildClass inherits strField from the ParentClass OBJECT, so it (ChildClass object) should have access to ParentClass object somehow, but how?

No, it is just a reusing the template of ParentClass to creating new ChildClass

By focusing on the business of non-arg constructor and compiler's involvement only, while the derived class( ChildClass )'s default constructor(non-arg constructor) is being invoked, a subobject of base class( ParentClass ) is created through the mechanism of compiler's help(insert base class constructor calls in the derived class) and wrapped within the derived class's object.

class Parent{
    String str = "i_am_parent";
    int i = 1;
    Parent(){System.out.println("Parent()");}
}
class Child extends Parent{
    String str = "i_am_child";
    int i = 2;
    Child(){System.out.println("Child()");}
    void info(){
        System.out.println("Child: [String:" + str + 
                           ", Int: " + i+ "]"); 
        System.out.println("Parent: [String: ]" + super.str + 
                           ", Int: " + super.i + "]"); 
    }
    String getParentString(){ return super.str;}
    int getParentInt(){ return super.i;}
    public static void main(String[] args){
        Child child = new Child();
        System.out.println("object && subojbect");
        child.info();
        System.out.println("subojbect read access");
        System.out.println(child.getParentString());
        System.out.println(child.getParentInt());

    }
}

result:

Parent()
Child()
object && subojbect
Child: [String:i_am_child, Int: 2]
Parent: [String: ]i_am_parent, Int: 1]
subojbect read access
i_am_parent
1

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