Differences between default constructor and no argument constructor
Default Constructor in java:
No-argument Constructor in java:
- When we write a class without any constructor then at compilation time java compiler creates a default constructor in our class.
- The accessibility modifier of the default constructor is same as accessibility modifier of class.
- The allowed accessibility modifier are public and default.
- Default constructor added by java compiler this constructor does not have anything except super(); call.
- package constructor;
- public class A {
- }
- package constructor;
- public class A {
- A(){
- super();
- }
- }
- If our class have any constructor then java compiler does not create default constructor
No-argument Constructor in java:
- As a developer we can create our own constructor with no arguments is known as no-argument constructor.
- It can have all four accessibility modifiers as it is defined by developer.
- So allowed accessibility modifiers are public, private, protected and default
- It can have logic including super call.
- package constructor;
- public class A {
- A(){
- super();
- System.out.println("no -argument constructor");
- }
- }
- The common point between default and no-argument constructor
- Both does not have any arguments.
- And one more point we need to remember that in no-argument constructor also by default first statement will be super() call which is added by java compiler if it does not have.
No comments