Inheritance: Access to base class fields from a su

2020-07-06 03:19发布

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?

6条回答
疯言疯语
2楼-- · 2020-07-06 03:27

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.

查看更多
趁早两清
3楼-- · 2020-07-06 03:30

Yes, you will be able to access the strField form the ChildClass, without performing any special action (note however that only one instance will be created. The child, which will inherit all properties and methods from the parent).

查看更多
可以哭但决不认输i
4楼-- · 2020-07-06 03:35

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.

查看更多
Emotional °昔
5楼-- · 2020-07-06 03:39

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

查看更多
贼婆χ
6楼-- · 2020-07-06 03:42

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
查看更多
不美不萌又怎样
7楼-- · 2020-07-06 03:53

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.

查看更多
登录 后发表回答