Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 946
user42155 Avatar asked Jan 28 '09 18:01

user42155


People also ask

How does a subclass invoke its superclass constructor?

To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.

Can a class inherit the constructor of its superclass Why?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

When you create any subclass object the subclass constructor executes first and then the superclass constructor executes?

When you create any subclass object, the subclass constructor must execute first, and then the superclass constructor executes. A nonstatic method cannot override a static member of a parent class. You cannot declare a class to be final.

Can a subclass inherit the constructor of a superclass?

No a subclass cannot inherit the constructors of its superclass. Constructors are special function members of a class in that they are not inherited by the subclass. Constructors are used to give a valid state for an object at creation.


2 Answers

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!");     } } 
like image 148
Kevin Avatar answered Nov 01 '22 17:11

Kevin


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.

like image 32
Outlaw Programmer Avatar answered Nov 01 '22 17:11

Outlaw Programmer