Java Programming Tutorial Index

Java Object Oriented

As you all know that all objects that are created must be given initial values which can be done in two ways. The first way of doing this is to use the dot operator to access the instance variable and then to assign values to them individually. It can be a tedious approach to initialize all the variables of the object in this manner.



The second approach takes the help of method like getData() or init() etc., to initialize each object individually using statements like:

val1.getdata(12,25);

Here, in this chapter, you will be dealing with constructors of Java, and learn about how they are used within a Java program and how they are useful Object Oriented Programming concept.

What are Constructors?

Constructors are special member functions whose task is to initialize the objects of its class. It is treated as a special member function because its name is the same as the class name. Java constructors are invoked when their objects are created. It is named such because, it constructs the value, i.e., provide data for the object, i.e., they are used to initialize objects. Every class has a constructor when we don't explicitly declare a constructor for any java class the compiler creates a default constructor for that class which does not have any return type. The constructor in Java cannot be abstract, static, final or synchronized and these modifiers are not allowed for the constructor.

Characteristics of Java Constructors

  • An interface cannot have the constructor.
  • Constructors cannot be private.
  • A constructor cannot be abstract, static, final, native, strictfp, or synchronized
  • A constructor can be overloaded.
  • Constructors cannot return a value.
  • Constructors do not have a return type; not even void.
  • An abstract class can have the constructor.
  • Constructors name must be similar to that of the class name inside which it resides.
  • Constructors are automatically called when an object is created.

Instance variables and methods of a class are known as members of a class. Constructors are not members. For this reason, constructors cannot be inherited; but can be accessed by a subclass. Java constructors do not get inherited; only their members (variables and methods) get inherited. So declaring a constructor as final is useless and has no meaning as constructors cannot be overridden. Again, Java constructors should not be synchronized as it locks the object when created and hence, as long as the object is not created any other object can be instantiated.

Types of Java Constructors

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor

Default Constructor

A constructor having no parameter is known as default constructor and no-arg constructor.

The structure of a default constructor is like this:

class Demo
{
   public Demo()
  {
   System.out.println("This is a default constructor");
  }
}

The syntax of default constructor:

Syntax:

<class_name>(){

}

Parameterized Constructor

A constructor having an argument list is known as a parameterized constructor. Parameterized constructors are used to supply dissimilar values to the distinct objects.

The structure of a parameterized constructor in Java is:

class Demo
{
 public Demo(int num, String str)
  {
   System.out.println("This is a parameterized constructor");
  }
}

Program for Default Constructor

Example:

import java.util.*;
import java.lang.*;
import java.io.*;
class clerk{
 int roll=101;
   String grade="Manager";
   void display(){System.out.println(roll+" "+grade);}
   public static void main(String args[]){
   clerk c1=new clerk();
   clerk c2=new clerk();
  c1.display();
  c2.display();
 }
}

Program for Parameterized Constructor

Example:

import java.util.*;
import java.lang.*;
import java.io.*;
 class paramC{
 paramC(int a, int b){
  System.out.print("Parameterized Constructor");
  System.out.println(" having Two parameters");
 }
  paramC(int a, int b, int c){
  System.out.print("Parameterized Constructor");
  System.out.println(" having Three parameters");
 }
  public static void main(String args[]){
  paramC pc1 = new paramC(12, 12);
  paramC pc2 = new paramC(1, 2, 13);
 }
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram