Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.

Here is an example of how this works:

class Foo {
    Foo(String str) { }
}

class Bar extends Foo {
    Bar(String str) {
        // Here I am explicitly calling the superclass 
        // constructor - since constructors are not inherited
        // you must chain them like this.
        super(str);
    }
}
Answer from Andrew Hare on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › IandI › super.html
Using the Keyword super (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the ...
🌐
O'Reilly
oreilly.com › library › view › java-a-beginners › 9780071606325 › ch7lev1sec4.html
Using super to Call Superclass Constructors - Java, A Beginner's Guide, 5th Edition, 5th Edition [Book]
Using super to Call Superclass Constructors A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here,... - Selection from Java, A Beginner's Guide, 5th Edition, 5th Edition [Book]
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit9-Inheritance › topic-9-2-constructors.html
9.2. Inheritance and Constructors — CSAwesome v1
If you are extending a class without a no-argument constructor but you want your class to have a no-argument constructor you will need to explicitly write one and use super to call an existing constructor on the superclass with appropriate arguments. However it is created, explicitly or implicitly, ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit10-Inheritance › topic-10-2-constructors.html
10.2. Inheritance and Constructors — CS Java
If you don't use super as the first ... no argument constructor. ... II is invalid. Children inherit all of the fields from a parent but do not have direct access to private fields. You can use super in a constructor to initialize inherited fields by calling the parent's constructor with the same parameter list. ... You can step through this code using the Java Visualizer ...
🌐
Reddit
reddit.com › r/javahelp › inheritance with constructors: "super" vs "this."
r/javahelp on Reddit: inheritance with constructors: "super" vs "this."
January 3, 2024 -

I am doing a lesson right now about inheritance with super/sub class with constructors. The lesson says there are 2 ways have child constructor inherit and parent one. The first is with super keyword and this works...

The parent class is below...

public class Shape {
    int sides;
    public Shape(int numSides) {
        sides = numSides;
        System.out.println("this obj has "+ sides +" sides.");
    }

and the child class using "super"

public class Triangle extends Shape {
    public Triangle() {
        super(3);
    }
    
    public static void main(String[] args) {
        Triangle tr = new Triangle();

which outputs when runs...

"this obj has 3 sides"

Now, the other way they said can do is bypass the parent constructor and reference the parent field using this. like below (parent class is unchanged)

child class using "this" keyword

public class Triangle extends Shape {
    public Triangle() {
        this.sides = 3;
    }

    public static void main(String[] args) {
        Triangle tr = new Triangle();

which i tried but get the error ..

"Implicit super constructor Shape() is undefined. Must explicitly invoke anotherconstructor".

it says the super() constructor is secretly called in this case by the compiler, so why is this error happening? What in this concept am i missing?

Thank you

Top answer
1 of 4
5
The only constructor Shape has is one that takes a single int argument. The implicit super-constructor call will only call super() without arguments. That way you can create a subclass of Object (which all classes are subclasses) without having to call super(), so it looks nicer (among other obvious uses). So in this case you either need to add a super constructor call with an int argument, or create a constructor in Shape (one that's also accessible from the Triangle class) that takes no arguments. Probably best to explicitly call super(3) as in your second code block. I guess it's also important to realize that when you don't define a constructor in a class, it has a public no-args constructor automatically created. As soon as you define any constructor, like the one in your Shape class, even if it's private, then the compiler won't generate a public no-args constructor. Java has so many weird little things like this that you don't think about until you run into them. the other way they said can do is bypass the parent constructor I think wording it like "bypass"ing a constructor, is a confusing way to describe setting the field of the parent in the child. Which also isn't something people generally do it real code. Best practice would be to use constructors. But I guess it depends. There's a lot of flexibility in Java that you generally won't need most of the time unless maybe you're a library author.
2 of 4
2
it says the super() constructor is secretly called in this case by the compiler, so why is this error happening? What in this concept am i missing? The super constructor looks like this: public Shape(int numSides), so it needs a numSides argument. Java would "secretly" call the super constructor if there were one with no arguments, but it has no idea what it should supply as the argument, so it can't call it implicitly and you have to actually call super(something).
🌐
Programiz
programiz.com › java-programming › super-keyword
Java super Keyword (With Examples)
So, why use redundant code if the compiler automatically invokes super()? It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor.
🌐
OpenJDK
openjdk.org › jeps › 447
JEP 447: Statements before super(...) (Preview)
In all of these examples, the constructor code that we would like to write contains statements before an explicit constructor invocation but does not access any fields via this before the superclass constructor has finished. Today these constructors are rejected by the compiler, even though all of them are safe: They cooperate in running constructors top down, and they do not access uninitialized fields. If the Java language could guarantee top-down construction and no-access-before-initialization with more flexible rules then code would be easier to write and easier to maintain. Constructors could more naturally do argument validation, argument preparation, and argument sharing without doing that work via clumsy auxiliary methods or constructors.
Find elsewhere
Top answer
1 of 7
268

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.

2 of 7
63

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

🌐
DEV Community
dev.to › arshisaxena26 › mastering-super-keyword-in-java-unlocking-inheritance-and-constructor-chaining-3ehm
Mastering SUPER Keyword in Java: Unlocking Inheritance and Constructor Chaining - DEV Community
November 6, 2024 - Calling Parent’s Constructor with super(): We use super(id, name, ...) to initialize common fields by invoking the parent class constructor. Constructor chaining is a powerful feature in Java that allows a subclass constructor to invoke a parent class constructor, ensuring that the parent class is properly initialized before executing the child class's constructor.
🌐
Belief Driven Design
belief-driven-design.com › looking-at-java-22-statements-before-super-5c833
Looking at Java 22: Statements before super | belief driven design
We might even run into these issues without subclassing at all, as Records don’t allow any argument modification before calling this(...) in a non-canonical constructor. No matter which route is taken, the resulting code tends to become more complex and less intuitive than desired. JEP 447 is going to remedy a lot of these pain points! The reason for requiring to call super(...) first is found in the Java Language Specification (JLS §8.8.7).
🌐
Reddit
reddit.com › r/learnjava › question about constructor in superclass and subclass
r/learnjava on Reddit: Question about constructor in superclass and subclass
August 14, 2024 -

I am currently learning Java and when going through the Java tutorials on using the super keyword, I got confused.

It is stated:

Note: 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. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

What does this actually mean? Suppose I have a superclass A and subclass B. There is a relationship Object <: A <: B.

class A {
  Public A(int x) {}
}

class B extends A {
  Public B(int x) {}
}

So in the above case, I did not explicitly invoke a superclass constructor and did not use the super keyword at all. Why would this give me an error if I just call the constructor of B normally new B(int)? I am pretty sure I am misinterpreting the statement in the tutorial here. Can someone please explain it in greater clarity?

Top answer
1 of 4
6
So the purpose of the constructor is to initialize an object. The most basic way of using it is to initialize any starting data values that an object relies on. If a class does not have a no-argument constructor, it is not possible to instantiate a new object without first inputting some values. Imagine you have a Person class, that has a String name data field. You want your code to operate such that any Person object you create must have a name, therefore you require that the user enters the name when they instantiate a new Person object: public class Person { String name; public Person(String name){ this.name = name; } } If you know the basics, it should be pretty clear that you will not be able to instantiate a new Person object without including a name: Person myPerson = new Person(); // will return an error Person myPerson = new Person("John"); //will create a Person object as desired Now, let's say we want to create another class that extends Person, for example Programmer. Let's try creating this class using a no-arg constructor: public class Programmer extends Person { String language = "Java"; public Programmer(){ //we'll get to this later } public void helloWorld(){ System.out.println("Hello world! My name is " + name + ". I can program in " + language); } } Now what happens if we try to instantiate a Programmer object, and run the helloWorld() function using it? The superclass Person has the name variable, so we should be able to access it in the subclass -- sharing data in this way is one of the major benefits of inheritance, after all. But the object that we've instantiated doesn't have a name associated with it. Hopefully, now it is starting to make sense why this wouldn't work. Instead, when we write the constructor for the Programmer class, we do it in this way: public Programmer(String name){ super(name); //this calls the constructor in Person, allowing us to initialize the name variable! } Does it now make sense why if the superclass lacks a no-argument constructor, you must use the super() call with the correct arguments in your subclass constructor? It's because otherwise, you have no way of initializing the variables in your superclass that you may want to use in your subclass. I hope that this is helpful, let me know if you have further questions.
2 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Medium
medium.com › @AlexanderObregon › how-javas-super-keyword-works-in-method-calls-and-constructors-2365efdc0f80
How Java’s super Keyword Works in Method Calls and Constructors
March 1, 2025 - A class can have multiple constructors with different parameters. The subclass can decide which one to call using super(arguments);.
🌐
GeeksforGeeks
geeksforgeeks.org › java › super-keyword
Super Keyword in Java - GeeksforGeeks
Note: The super keyword is a key feature of inheritance and polymorphism in Java and it provides several benefits for developers seeking to write reusable, extensible and well-organized code. ... Call to super() must be the first statement in the Derived(Student) Class constructor because if you think about it, it makes sense that the superclass has no knowledge of any subclass, so any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass.
Published   July 23, 2025
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › language › statements-super.html
Statements Before super(…)
March 13, 2024 - The epilogue of the constructor's body consists of the statements that follow the super(...) invocation. The preconstruction context of a constructor consists of the arguments to an explicit constructor invocation, such as super(...), and any statements before it.
🌐
Delft Stack
delftstack.com › home › howto › java › super constructor java
Super Constructor in Java | Delft Stack
October 12, 2023 - Unlike the no-argument constructor automatically calls the super(), the parameterized constructor does not call it, and we need to call it with the arguments.
🌐
Coderanch
coderanch.com › t › 535754 › java › subclass-call-constructor-super-class
Must subclass call constructor of super class (Beginning Java forum at Coderanch)
You don't have to call a superclass constructor explicitly; it is not necessary to always have a super() or super(arguments) call in a subclass constructor. If you do not specify it, the compiler will automatically add a call to the no-arguments superclass constructor.
🌐
nipafx
nipafx.dev › inside-java-newscast-62
Java 22 Previews Statements Before super(...) and this(...) - Inside Java Newscast #62 // nipafx
In the case of class inheritance, chaining is enforced: A subclass constructor must call a superclass constructor with a super($ARGUMENTS) statement.
Published   February 1, 2024