Search code examples
javainheritancenestedsubclassinner-classes

Difference in creation between Inner and Subclass


I noticed that to make Inner class we need instance of Outer class. But to also make a Subclass, we need Parent class to create instance, as well.

What is difference between them if there is some? Strictly speaking in creation and not how they work. Can I picture it: there is object of Parent and inside it is Child object, same as Inner class object is inside Outer class's object in heap? Am I any close to this?


Solution

  • Maybe a picture/analogy to represent the two relationships:

    enter image description here

    i.e.:

    • janeDoe the subclass instance is just one object, but it's both types in one instance.
    • johnDoe, the inner class instance, is distinct from janeDoe, the outer class instance. They have a very close link and, between them, they know how they use each other.