2

We have below code:

class Parent {
    public int i = 5;
}

class Son extends Parent {

}

public class MyTest {

    public static void main(String[] args){
        **Son son = new Son();**
        System.out.println(son.i);

    }

}

When execute Son son = new Son(), below questions confused with me:

  1. Will create Parent instance in memory or not? I know that before create Son instance, will invoke Parent default Constructor with no parameter, is that correct invoking Constructor and create instance are different things?
  2. If did not create Parent instance, but where is field i store in Son instance? Cause we can get value of i via son reference?

Many thanks!!!

Tom
  • 21
  • 1
  • Does this answer your question? [superclass storing a subclass](https://stackoverflow.com/questions/19738996/superclass-storing-a-subclass) – Rafaqat Ali Dec 26 '19 at 02:16

3 Answers3

2

Will create Parent instance in memory or not? I know that before create Son instance, will invoke Parent default Constructor with no parameter, is that correct invoking Constructor and create instance are different things?

An instance of Parent will not be created, and indeed, the constructor of Parent will be invoked. Constructors are just special blocks of code for the JVM to run when creating an instance. There's no rule that says, "when creating an instance of T, only the constructor of T can be run".

If did not create Parent instance, but where is field i store in Son instance? Cause we can get value of i via son reference?

Just because it's not written in the source code, doesn't mean it's not there. By writing Son extends Parent, you are telling the compiler and the runtime that (among other things) Son does have a field called i, so you don't have to declare it again in Son. This is one of the main aims of inheritance - reduce duplicate code.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

When you create an instance of a subclass, a superclass constructor must be called to initialize the superclass fields, which in this case is son.i. When there is no constructor given for the superclass, Java implicitly creates a default constructor, equivalent to the below code:

public Parent() {}

When Parent is extended, the constructor of Son begins by calling the constructor of Parent, implicitly done via the below code:

public Son() {
    super() // Calls Parent()
}

In short, yes. A subclass does generate fields for the superclass in the memory. Think of a subclass as a parent class with a few bells and whistles. For example:

Parent myObj = new Son();
System.out.println(myObj.i); // Still prints 5 because i is still a variable of son
MAO3J1m0Op
  • 423
  • 3
  • 14
0

Questions 1: Yes, Java will automatically create a default Parent constructor if you haven't created one already and each time a new Son object is created, the Parent constructor will be called too. You can explicitly call the Parent constructor of your choice (assuming you've defined one or more) by making a call to super(...) in the first line of the Son constructor.

Question 2: The field 'i' is part of the Parent class. The way you've written it at the moment (with public access) it will be accessible to both the Son class and everyone else who wants to change it.

A further comment: It is good programming practice to make fields private to a class and, if possible, final.

hermit
  • 56
  • 6