Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?

37,198

Solution 1

It doesn't create two objects, only one: B.

When inheriting from another class, you must call super() in your constructor. If you don't, the compiler will insert that call for you as you can plainly see.

The superclass constructors are called because otherwise the object would be left in an uninitialized state, possibly unbeknownst to the developer of the subclass.

Your subclass actually looks like this after the compiler inserts the super call:

public class B extends A {
    public B() {
        super();
        System.out.println("Bye!");
    }
}

Solution 2

It doesn't create 2 objects, it only creates one instance of B. The reason the super class constructor is invoked is because, like you said, B has all of the fields of A, and these fields need to be initialized.

Solution 3

Remember inheritance is an "is a" relationship between the base class and the subclass, thus every time you have an instance of a subclass, by definition you will also have an instance of the base class (as part of the instance, not as two separate instances). To initialize the base class properly the constructor is called.

Additionally, think about what would happen if you subclass depended on some internal state of the base class. Wouldn't you want the instance of the base class to be initialized then?

Solution 4

This is done because the constructor is used to initialize the object. Since B is also an A, it calls the constructor for A first, then the constructor for B.

As a side note, you can use super(arg1, etc) to choose which constructor of A is called based on the parameter types you pass... but it must be the first line in the constructor.

Solution 5

It does not create two objects, it just creates one object b. b is of type B and of type A. A constructor is basically saying here is what you need to do to construct me. So when you are creating a new "B" instance, you are building an object that is both a B() and an A(). Imagine the following scenario:

class Q {
  int i;
  public Q() {
    // set default value
    i= 99;
  }
}

class Z extends Q {
  public Z() {
  }
}

If the constructor for Q WAS NOT called, how would i get its default value?

Share:
37,198

Related videos on Youtube

user42155
Author by

user42155

Updated on July 09, 2022

Comments

  • user42155
    user42155 almost 2 years

    I have a question about inheritance in Java.

    I have two classes A and B , and class B, inherits from A:

    public class A {
         public A() {
             System.out.println("Hi!");
         }
    }
    
    
    public class B extends A {
         public B() {
             System.out.println("Bye!");
         }
    
         public static void main(String[] args) {
             B b = new B();
         }
    }
    

    When I run program B, the output is:

    Hi!
    Bye!
    

    Question : why the constructor of class A is invoked, when I create and object of class B ?

    I know that B inherits everything from A - all instance or class variables, and all methods, and in this sense an object of B has all characteristics of A plus some other characteristics defined in B. However, I didn't know and didn't imagine that when I create an object of type B, the constructor of A is also invoked. So, writing this:

    B b = new B();
    

    creates Two objects - one of type B, and one of type A.

    This is getting interesting,

    can somebody explain why exactly this happens?

  • John Gardner
    John Gardner over 15 years
    It isn't 2 objects. If you were to say it in words, "B is an A". A car is an automobile. It isn't 2 objects.
  • Steve Kuo
    Steve Kuo over 15 years
    Class A needs a chance to initialize itself.
  • Tim Frey
    Tim Frey over 15 years
    I think the fundamental misunderstanding here is whether or not the call to super() is creating a new object. It isn't.