1

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?

Stefan
  • 969
  • 6
  • 9
  • 1
    @Hades Nope, I just came from that question.. I am targeting creation and objects, not functionality. – Stefan Oct 07 '20 at 05:05

1 Answers1

2

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.
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 1
    Awesome answer, thanks mate! Only one little concern (in subclass example). Why is super class constructor always called when there is no super class object in your diagram? Aren't constructors responsible for creating new object? So why is there no `new Mother()` as well? – Stefan Oct 07 '20 at 06:36
  • @Stefan I only mentioned constructor calls that we make explicitly when creating objects (i.e., I omitted all other constructor calls made by the class itself, explicitly or not). Think of it as instantiations (one instance is created irrespective of how many local or inherited constructors will run). – ernest_k Oct 07 '20 at 06:42
  • Ah I think I get it, but not totally. Sorry, I am not so good at this :) But if you had made a complete picture, there would have to be Mother object when creating Daughter? – Stefan Oct 07 '20 at 06:47
  • 1
    @Stefan no need to be sorry. I knew there was a risk of the picture showing some ambiguity. The best way to think about the left side is *a daughter picks up all the genes and behaviors of a mother by virtue of being a woman too, but a daughter can stand alone as a person*. But a fetus cannot stand alone (a mother instance is needed), and the fact that a fetus can have genes/behaviors of the mother is irrelevant in this case - that's why I made it `johnDoe`, a male :-) . I'll change Mother/Daughter to something more precise. – ernest_k Oct 07 '20 at 06:58
  • 1
    @Stefan I think the woman/mother analogy is more accurate than mother/daughter. I've updated the picture. – ernest_k Oct 07 '20 at 07:03