NOTE

OOP

Whether creating a subclass object also creates a parent object, whether private fields are inherited, and references.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. When Creating a Subclass Object, Is a Parent-Class Object Also Created?

No.

The super keyword simply calls the parent-class constructor to initialize properties.

2. Does a Subclass Inherit the Parent Class’s Private Variables?

From the concept of inheritance, private and final are not inherited. This is how the official Java documentation describes it.

From the memory perspective, everything in the parent class is inherited (this can be seen from the parent constructor being called: when an object is created with new, the constructor is called; when a subclass is created, the parent constructor is called. Therefore, from the memory perspective, the subclass contains a complete parent-class portion).

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub