-6

In the following example which constructor of the class U calls invokes the super constructor? The one without arguments or the one with one argument ?

public class T{
    T(){
        System.out.println("T()");
    }
       public static void main(String[] args){
        new U();
    }
}

class U extends T{
    U(){
        this(2);
        System.out.println("U()");
    }
    
    U(int x){
        System.out.println("U(int x)");
    }
}
  • I do not understand the question you have the code, you can debug/run It and seems a copy of https://stackoverflow.com/a/34488527/1511193 – vmrvictor Jun 24 '20 at 08:42
  • 2
    Does this answer your question? [Why is super class constructor always called](https://stackoverflow.com/questions/34488484/why-is-super-class-constructor-always-called) – vmrvictor Jun 24 '20 at 08:49

1 Answers1

1

The first constructor U() calls the second constructor U(int x) via the this(2) call.

The second constructor U(int x) calls the super class constructor T() implicitly.

Which means both U constructors call the super class constructor, either directly or indirectly.

Eran
  • 387,369
  • 54
  • 702
  • 768