devdoe
devdoe

Reputation: 4360

calling hierarchy in constructors of super and base class

class a
{
a(){System.out.println("A");}
}

class b extends a
{
b()
{
super();
System.out.println("B");}
}

class c extends b
{
c(){System.out.println("c");}
}

class last
{
public static void main(String aaa[])
{
c obj = new c();
}
}

Output Comes as:

A

B

C

Shouldn't it be:

A

A

B

C

because of super keyword

Upvotes: 3

Views: 4481

Answers (5)

Kultaran Singh
Kultaran Singh

Reputation: 1

package threaddemo;

public class NewClass {
    NewClass() {
        System.out.println("hello");
    }
}

class Child extends NewClass {

    Child() {
        System.out.println("child");
    }

    public static void main(String []ar) {
        Child c1=new Child();
    }
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075159

No. If you have a call to super within the constructor, the automatic call doesn't get added. The compiler only adds the automatic call if you leave yours out. So the super(); line in b is unnecessary, as that's exactly what the compiler will add for you (a call to the default constructor). That is, these two bits of source result in identical bytecode:

// This
class b {
    b() {
    }
}

// Results in the same bytecode as this
class b {
    b() {
        super();
    }
}

The reason for being able to call the superclass constructor directly is for passing arguments to it, since the compiler will only ever add calls to the default constructor (and will complain if there isn't one on the superclass).

Upvotes: 4

Juvanis
Juvanis

Reputation: 25950

super(); is called once within any constructor through inheritance tree, either you do it explicitly or it is done implicitly. So you shouldn't expect that "A" will be printed twice.

This won't compile:

b()
{
    super();
    super();
    System.out.println("B");
}

Error message: Constructor must be the first statement in a constructor.

It means that you are not allowed to call super() multiple times in a constructor.

Upvotes: 2

Lauri
Lauri

Reputation: 1899

If you create sub class of some other class, with extends keyword, then java compiler puts super() call as first line of your constructor (in case you have not done that yourself). In your example, the class a extends (by default) java.lang.Object() and after compilation the first line is call super() which calls Object default constructor. SO before code in sub class constructor is ran, the code in its super class constructor is ran.

Why is A not printed multiple times? Because Java compiler adds super() in the beginning of constructor only if you have not done that yourself. (For example, you might want to call super class constructor that takes in some parameters)

Hopefully that clarifies things a little bit.

Upvotes: 0

Amit Deshpande
Amit Deshpande

Reputation: 19185

super(); is always there if you don't specify explicitly. Java only adds automatic call if you don't specify it explicitly.

So your code

    B() {
        super();
        System.out.println("B");
    }

is same as

    B() {
        System.out.println("B");
    }

Upvotes: 8

Related Questions