By ajsie


2010-01-13 02:42:58 8 Comments

Isn't this one automatically put by the compiler if I don't put it in a subclass's constructor?

That means I don't even need to care about it? In some articles they put it out.

And if I've got one constructor with arguments, will this be the constructor, or does it take a constructor without argument list?

6 comments

@Roland Illig 2016-12-11 10:46:11

Calling the no-arguments super constructor is just a waste of screen space and programmer time. The compiler generates exactly the same code, whether you write it or not.

class Explicit() {
    Explicit() {
        super();
    }
}

class Implicit {
    Implicit() {
    }
}

Update (December 2018):

Writing an explicit super() helps navigating the source code in the IDE.

As of December 2018, neither Eclipse nor IntelliJ provide any means of comfortably navigating from the constructor of the derived class to the constructor of the base class.

@John Henckel 2017-05-26 02:39:50

Best answer. Direct and correct.

@Jon 2018-02-27 14:56:35

The empty no-args Implicit constructor is also unnecessary.

@Eric Wilson 2018-05-22 17:00:12

@Jon In this case, yes. But if you had a constructor with arguments, then the no-arg constructor will not be created by default.

@Yan Khonski 2017-06-29 12:21:35

Default parent constructor is called from child default constructor even if you do not call it.

Main

public class Main {

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

A

public class A {

    public A() {
        System.out.println("A");
    }
}

B

public class B extends A {

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

Prints

A
B

@cletus 2010-01-13 02:48:01

Firstly some terminology:

  • No-args constructor: a constructor with no parameters;
  • Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and
  • Default constructor: the public no-args constructor added by the compiler when there is no explicit constructor in the class.

So all classes have at least one constructor.

Subclasses constructors may specify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified.

For example:

public class Base { }
public class Derived extends Base { }

This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.

public class Base { }
public class Derived extends Base { public Derived(int i) { } }

Also fine.

public class Base { public Base(String s) { } }
public class Derived extends Base { }

The above is a compilation error as Base has no default constructor.

public class Base { private Base() { } }
public class Derived extends Base { }

This is also an error because Base's no-args constructor is private.

@flow2k 2017-07-31 08:09:19

So, I guess the answer to the OP's question in the title is "yes, it is unnecessary to include super() in the child constructor", because super() is a call to the "accessible no-args constructor".

@Anup Verma 2017-08-26 15:38:49

"The above is a compilation error as Base has no default constructor." should be modified to "The above is a compilation error as Base has no no-args constructor."

@ahsan.dev 2018-05-17 00:26:50

Depends on how you think. I think while it is not necessary - i would not say its unnecessary.

@Dharmendrasinh Chudasama 2017-02-16 04:36:10

Any class constructor always calls "super()" if there is not explicitly called super([arguments]), only we keep in mind access of super class constructor while programming... when we not extends any specific class automatic extends java.lang.Object class

@abhishek ringsia 2016-01-14 17:50:19

abstract class Book
 {
String title;
String author;
Book(String t,String a){
    title=t;
    author=a;
}
abstract void display();

}    

If super class can have a No-args constructor .It is good to have a no argument constructor otherwise you have to pass super constructor with parameter .

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified

class MyBook extends Book{   
int price ;
public  MyBook(String t,String a,int price){
     super(t,a);
    this.price=price;
}

public void display(){
    System.out.println("Title: "+title);
 System.out.println("Author: "+author); 
System.out.println("Price: "+price);

}

}

@user207421 2016-11-30 02:56:03

First paragraph is a non sequitur.

@lemon 2010-01-13 02:47:34

If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.

src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html

@JonyD 2017-04-13 11:48:50

...and if super class has no constructor whatsoever, it [the default constructor] will also be provided by the compiler. The provided constructor will have the same access level as the super class access level.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] What is PECS (Producer Extends Consumer Super)?

9 Answered Questions

[SOLVED] Call one constructor from another

  • 2010-10-24 16:06:31
  • Avi
  • 432371 View
  • 925 Score
  • 9 Answer
  • Tags:   c# constructor

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 807022 View
  • 2134 Score
  • 21 Answer
  • Tags:   java constructor

19 Answered Questions

[SOLVED] Why do this() and super() have to be the first statement in a constructor?

  • 2009-07-22 21:25:15
  • Joe Daley
  • 212199 View
  • 570 Score
  • 19 Answer
  • Tags:   java constructor

10 Answered Questions

[SOLVED] Calling the base constructor in C#

17 Answered Questions

[SOLVED] Interface defining a constructor signature?

17 Answered Questions

[SOLVED] Virtual member call in a constructor

5 Answered Questions

15 Answered Questions

[SOLVED] Can I call a constructor from another constructor (do constructor chaining) in C++?

  • 2008-11-21 09:43:08
  • Stormenet
  • 433214 View
  • 876 Score
  • 15 Answer
  • Tags:   c++ constructor

Sponsored Content