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.

Related Posts

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    Table of ContentsSyntaxExampleUse multiple static blocks In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class […]

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.