Java default constructor

In this post, we will see about Java default constructor.
Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly.
You won’t able to see it as it is present in class file rather than source file.

Is there any difference between no argument constructor and default constructor?

If you provide any constructor in class, it’s no longer default constructor.There is lots of debate on it but it is my opinion.
When you do not provide any constructor, compile will insert default constructor which will call super class’s default constructor.You need to make sure that super class has no-arg constructor.

Let’s understand this with the help of an example
Create a class named Person.java

Create another class named Employee.java

You will get compilation error at line with this message.
"Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor"
As you can see here, it tells you that Person class should have explicit constructor else it won’t compile.
Once you add no arg constructor to Person class, you won’t get compilation error anymore.

As you can see once you add explicit constructor to Person class, there won’t be any compilation error in Employee class.
That’s all about default constructor in java.

Was this post helpful?

Related Posts

  • 29 November

    How to Get Variable From Another Class in Java

    Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.