Java Constructor | Types of Constructor in Java with Examples.

Java Constructor



What is Constructor in Java?

A constructor is a Functionality (or) method which is Executed automatically at the time of the creation of the Object.

Why we require Constructor in Java?

A constructor is required for creating and initializing Object in Java.

Rules to define Constructor:

1  1)    Constructor name should be same as the Class name.
    2)    A constructor should not contain modifiers.
    3)    A constructor cannot contain return type.
    4)    In Constructor logic return statement with the value is not allowed.
    5)    The constructor must be called along with “new” keyword or else it leads to a compile-time error.

Syntax to declare Constructor:

<Class Name>()
{
}

Example:


Class A
{
 A()
{
System.out.println(“Constructor Example”);
}
public static void main(String[] args)
{
 A a1 = new A();
}
}



 Output: Constructor Example.

Types of Constructors: There are “3” types of constructors in Java. They are:

1) Default Constructor.
2) No-argument Constructor (or) Non-parameterized Constructor.
3) Parameterized Constructor.

Default Constructor:
 The Zero-argument (or) No-argument Constructor generated by Compiler is called as Default Constructor.
                                                          
In detail: At the time of Compilation, compiler checks whether the class has at least one constructor or not if the compiler finds that the class doesn’t have at least one constructor then compiler automatically provides a Zero argument (or) No-argument constructor to the class is called as Default constructor.

Example of Default Constructor:

class A
{
 int i,j;
 void funA()
 {
  System.out.println("FunA() of A");
 }
}


To see Compiler generated constructor.

    1)    First, write any Java program without Constructor.
    2)   Compile the Java program in command prompt (Example using above    Class A program (type javac <Class name>.java) in command prompt).
 Example: javac A.java
   3)   To see default constructor (type javap <class name>) in command prompt.
Example: javap A

Compiler generated output: 


Class A
{
   int i;
   int j;
   A();                
   Void funA();
}


In the above Compiler generated output A() is the default constructor.

Why compiler-generated Constructor is called as Default Constructor?

     A)   It Obtains all its default properties from its Class. They are:
1)    Its name is the same as its class name.
2)    It does not have parameters and logic.
3)    Its accessibility modifier is the same as its class accessibility modifier.

No-argument Constructor (or) Non-parameterized Constructor: The developer generated constructor without parameters is called as No-argument Constructor (or) Non-parameterized Constructor. A Constructor is compulsory in a class but No-argument Constructor is not mandatory in a class.

Example of No-argument Constructor:


class B
{
 int i,j;
 B()         
 {
  System.out.println("FunB() of Class B");
 }
 public static void main(String[] args)
 {
  B b1 = new B();
 }
}

Output:

FunB() of Class B

Difference between the No-argument and Default Constructor?

Default Constructor:
1)     It is generated by the compiler.
2)     Its accessibility modifier is same as class accessibility modifier. So the only allowed accessibility modifiers are default or public.
3)     It does not have logic except super() call.
No-argument:
     1)    It is defined by the developer.
     2)    It can have all four accessibility modifiers as it is defined by the developer, so the allowed accessibility modifiers are private, default, public, protected.
     3)    It can have logic including super() call.


Parameterized Constructor: The developer generated Constructor with parameters is called as Parameterized constructor. The parameterized constructors may contain logic also.

Example:


class C
{
 int a,b;
 C(int x)
 {
  a = x;
  b = x + 1;
  System.out.println("Example of Parameterized constructor");
  System.out.println("c(int x)");
 }
 C(int x, int y)
 {
  a = x;
  b = y;
 System.out.println("c(int x, int y)");
 }
 C(boolean f)
 {
 System.out.println("c(boolean f)");
 }
public static void main(String[] args)
{
 C c1 = new C(3);
 System.out.println("a = " + c1.a + " b = " + c1.b);
 System.out.println();
  C c2 = new C(3,5);
 System.out.println("a = " + c2.a + " b = " + c2.b);
 System.out.println();
 C c3 = new C(true);
 System.out.println("a = " + c3.a + " b = " + c3.b);
}
}

output:


Example of Parameterized constructor
c(int x)
a = 3 b = 4

c(int x, int y)
a = 3 b = 5

c(boolean f)
a = 0 b = 0

Constructor overloading:  Defining multiple constructors within the same class by changing the data types of the parameters is called Constructors overloading. Like methods, constructors can also be overloaded but they cannot be overridden because constructors are not inherited, we cannot create a constructor in a subclass with superclass name.
The rules applicable in static polymorphism all those rules are applicable in Constructor overloading also.

Example of Constructor overloading:


class Conexample
{
 Conexample()
 {
 System.out.println("Example of constructor overloading");
  System.out.println("Example of No-argument constructor overloading");
 }
 Conexample(int x)
 {
  System.out.println("Example of int argument");
 }
 Conexample(String s1)
 {
  System.out.println(s1 + " Example of String argument");
 }
public static void main(String[] args)
{
 Conexample c1 = new Conexample();
 Conexample c2 = new Conexample(7);
 Conexample c3 = new Conexample("Tutorials Web");
}
}


Output:

Example of constructor overloading
Example of No-argument constructor overloading
Example of int argument
Tutorials Web Example of String argument


Calling one constructor from another Constructor: We can call a Constructor from another Constructor using “this” Keyword.
Example:
class Cdemo
{
 Cdemo()
 {
  this(2);
  System.out.println("Cdemo()");
 }
 Cdemo(int x)
 {
  this(x,x+2);
  System.out.println("Cdemo(int x)");
 }
 Cdemo(int x,int y)
 {
  System.out.println("Cdemo(int x,int y)");
 }
public static void main(String[] args)
 {
  Cdemo c = new Cdemo();
 }
}

Rules for calling Constructor Explicitly from another Constructor:

1) The statement which explicitly calls a constructor must be the first statement in a Constructor.
2) The statement which explicitly calls a constructor should be present only inside a constructor and it cannot be called through a method.
Constructor Chaining: Calling one constructor from another constructor using super() and this() is called as Constructor Chaining. Subclass constructors are chained with superclass constructors by using super() and subclass overloaded constructors are chained by using this().
Example:
class Cdemo2
{
 Cdemo2()
 {
  this(8);
  System.out.println("Constructor chaining Example");
  System.out.println("No-argument constructor");
 }
 Cdemo2(int i)
 {
  this("Tutorials web");
  System.out.println("Parameter constructor");
 }
 Cdemo2(String s)
 {
  System.out.println("String argument constructor");
 }
}

class Cdemo3 extends Cdemo2
{
 Cdemo3()
 {
  this(8);
  System.out.println("No-argument constructor");
 }
 Cdemo3(int i)
 {
  this("Tutorials web");
  System.out.println("Parameter constructor");
 }
 Cdemo3(String s)
 {
  System.out.println("String argument constructor");
 }
}
class Ctest
{
 public static void main(String[] args)
 {
  new Cdemo3();
  new Cdemo3(7);
  new Cdemo3("Tutorials Web");
 }
}

Output:

String argument constructor
Parameter constructor
Constructor chaining Example
No-argument constructor
String argument constructor
Parameter constructor
No-argument constructor
String argument constructor
Parameter constructor
Constructor chaining Example
No-argument constructor
String argument constructor
Parameter constructor
String argument constructor
Parameter constructor
Constructor chaining Example
No-argument constructor
String argument constructor.
Share:

Our Services

Search This Blog

  • ()
Powered by Blogger.

C Data Types and Types of data types in C

Data types specify the type of data that a variable can store. All C compilers support Five fundamental data types, they are an integer(int...