Selasa, 15 Maret 2016

Differences between Default constructor and no argument constructor in java

Differences between default constructor and no argument constructor


Default 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.





  1. package constructor;
  2. public class A {
  3.   
  4.  
  5. }


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.         super();
  7.  
  8.     }
  9.  
  10. }



  • 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.


  1. package constructor;
  2. public class A {
  3.  
  4.     A(){
  5.         
  6.   super();
  7.   System.out.println("no -argument constructor");

  8.     }
  9.  
  10. }

  • 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.

Bagikan

Jangan lewatkan

Differences between Default constructor and no argument constructor in java
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.