简体   繁体   中英

When to call Java super constructors?

Okay, so I'm reading this Java book "Java in 21 Days" and the author makes some of what I feel are the most confusing claims in any programming book I've read. I think the author just does a very poor job of articulating the points. Here are the two most confusing points and I would really appreciate it if some experienced java developers could explain them more clearly.

Point 1:

Constructors cannot be overridden. Because a constructor always has the same name as the current class, new constructors are created instead of being inherited. This is sufficient much of the time; when your class's constructor is called, the constructor with the same signature for all your superclasses also is called. Therefore, initialization can happen for all parts of a class you inherit.

I think I understand what he's saying here. You have to use the super( ) constructor within the subclass constructor to initialize any of the necessary attributes of the superclass. That is, if the superclass constructor requires arguments, you can pass these arguments via super( ). But he absolutely loses me when you says "when your class's constructor is called, the constructor with the same signature for all your superclasses also is called". The line immediately before that, he mentions how constructors are not inherited because they always have the same name as the class; so, what does he mean that the constructor with the same signature in the superclass will be called? None of the superclass constructors will have the same signature as the constructors of the subclass because they have different names. Can someone clarify?

The author makes a similar statement in point 2:

You don't have to call the constructor in your superclass that has the same signature as the constructor in your class; you must call the constructor only for the values you need initialized. In fact, you can create a class that has constructors with entirely different signatures from any of the superclass's constructors.

,?. Again? can someone clarify here, He says "create a class that has constructors with entirely different signatures from any of the superclass's constructors", Isn't this the case implicitly. The constructors necessarily must have different names, and therefore different signatures, from all of their superclasses. Again, I would really appreciate an experience Java developer's explanation here, because I am very confused.

The author doesn't even provide any code to illustrate these points. If you could provide some code with your explanations that would be incredibly productive for me. Thanks. Let me know if I need to clarify or add more context.

The text you are reading is misleading or wrong.

What it sounds like it is trying to say is that if you have a class A with a constructor A(int) and a subclass B of A with a constructor B(int) then that constructor will automatically call the A(int) constructor.

But that is not true. Any constructor you write in a subclass will implicitly call the no-argument constructor of its superclass, unless you explicitly call super(...) .

So

class B extends A {
    B(int value) {
        // implicitly calls the no arguments constructor, A()
    } 
}

If a no-arguments constructor is not available (non-existent or private) then you have to call super(...) in your subclass constructor, giving arguments that match an available constructor in your superclass.

From https://docs.oracle.com/javase/tutorial/java/IandI/super.html :

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

By default, subclass constructor will make a call to super class constructor with no argument. This is implicit.

class A {

}

class B extends A{
   int b;
   public B(int bParam) {
      // implicit super()  call to A
      b = bParam;
   }

}

But suppose your super class doesn't have any no-argument constructor & you want to initialize some superclass variable, you will have to make a explicit super() call with arguments.

class A {
   int a;
   public A(int aParam) {
      a = aParam;
   }

}

class B extends A{
   int b;
   public B(int aParam, int bParam) {
      //initialize a field in super class A
      super(aParam);
      b = bParam;
   }
}

So below two comments are not true. It is not about same signature constructor. Only no-arg constructor from super class will be called.

This is sufficient much of the time; when your class's constructor is called, the constructor with the same signature for all your superclasses also is called.

You don't have to call the constructor in your superclass that has the same signature as the constructor in your class; you must call the constructor only for the values you need initialized

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM