-1

Assume the following code. I don't understand why the output is B's constructor is invoked A's constructor is invoked.

I thought the output was just "A's constructor is invoked", because the construction A(int t) don't invoke the constructor B ?

public class Test {
    public static void main(String[] args) {
        A a = new A(3);
    }
}

class A extends B {
    public A(int t) {
        System.out.println("A's constructor is invoked");
    }
}

class B {
    public B() {
        System.out.println("B's constructor is invoked");
    }
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
Anna Saabel
  • 129
  • 5
  • 2
    no code -> no constructor is invoked – Stultuske May 14 '19 at 10:00
  • what code? where is it? [mcve] – Lino May 14 '19 at 10:00
  • 3
    @Anna the very first thing a constructor does, is invoking the constructor of it's parentclass, whether you specifically code it to do or not. The only difference with specifically calling the parent constructor, is that you can choose which constructor of the parent class is called – Stultuske May 14 '19 at 10:01
  • Reference : https://dzone.com/articles/constructor-chaining-in-java – Arun Sudhakaran May 14 '19 at 10:04
  • @Stultuske But what happens to input 3? B only has a constructor without arguments and if I have a constructor with an int as argument for B the constructor is still chosen without arguments. How can I prevent A from calling the constructor of B ? – Anna Saabel May 14 '19 at 10:06
  • 1
    @AnnaSaabel the only way to prevent the constructor from A to call the constructor of B, is by not allowing A to extend B – Stultuske May 14 '19 at 10:07
  • 1
    @Anna Saabel always remember that the first line of a constructor will be a call to its super class constructor, even if you don't keep it compiler will keep it in the `.class` file. If you keep one explicitly then compiler will be calling that one other wise it will call the no-arg constructor by default. – Arun Sudhakaran May 14 '19 at 10:13

3 Answers3

0

Whenever the derived class’s default constructor is called, the base class’s default constructor is called automatically and same rule follows for the parameterised constructor.That's why You are getting the output : B's constructor is invoked . A's constructor is invoked.

AJEET BAL
  • 1
  • 2
0

super() is added in each class constructor automatically by compiler.

Default constructor is provided by compiler automatically but it also adds super() for the first statement as shown in image. So constructor of base is invoked first.

Refer to below picture to understand it.

Shyamk327
  • 26
  • 7
  • "Default constructor is provided by compiler automatically", no, it isn't. – Stultuske May 14 '19 at 10:13
  • I read it in many sites. Please provide correct resource to understand it . @Stultuske [http://www.javawithus.com/tutorial/default-constructor-provided-by-the-compiler] – Shyamk327 May 14 '19 at 10:21
  • 1
    providing a dead link won't win an argument. The compiler will add a default constructor, if and ONLY IF, there is not another constructor already present. So, in the code of the OP, the compiler will not provide any default constructors. Class B already contains one, and class A contains a parametrized constructor – Stultuske May 14 '19 at 10:24
  • just went to that link (your error is you added the closing ] in the url). The source is not trustworthy. The author assumes that all final variables are constants, while in a lot (if not most) situations, that is not the case. – Stultuske May 14 '19 at 10:30
  • Got it. Thank you. :) – Shyamk327 May 14 '19 at 10:34
  • When you are not sure about something regarding java language or java behaviour: Read it up inside the Java Language Specification (JLS) and/or Java Virtual Machine Specification(JVMS). They can be found at https://docs.oracle.com/javase/specs/ and the default constructor is describes inside the JLS chapter 8.8.9. – Konrad Neitzel May 14 '19 at 10:57
0

If you don't call any superclass constructor in your subclass constructor, a default (no-args) constructor call will be implicitly added as the first statement in your subclass constructor:

class A extends B {
    public A(int t) {
        super(); // Added by compiler
        System.out.println("A's constructor is invoked");
    }
}

If the superclass doesn't have a no-args constructor (whether explicitly defined or an implicit one), you'll get a compiler error, meaning that you'll have to explicitly call an existing superclass constructor as the first statement in your subclass constructor.

Implicit super constructor B() is undefined. Must explicitly invoke another constructor

An implicit no-args constructor is added to a class by compiler if it has no explicitly defined constructors.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43