Java constructor

Created with Sketch.

 

Java constructor

A Java constructor is a method that is used to initialize an object. It has the same name as that of the class and is called or invoked when we create an object of the class, and we can’t call them explicitly. While creating an object, its attributes may or may not be available. If none is available, then the default constructor is called. It is optional to write a constructor method(s) in a class, but due to their utility, we use them.

Java constructor example

class Programming {
//constructor method
Programming() {
System.out.println(“Constructor method called.”);
}public static void main(String[] args) {
Programming object = new Programming(); // Creating an object
}
}

The output of the program:
Java constructor example program output
The program is the simplest example of a constructor. The constructor gets called when we create an object of the class Programming. As you can see, the output prints the “Constructor method called.”

Java constructor overloading

Constructors in Java can be overloaded just like other methods, i.e., we can create as many constructors in a class as required. The number depends on the information we have about the attributes of an object while creating it. Constructor overloading program example:

class Language {
String name;Language() {
System.out.println(“Constructor method called.”);
}Language(String t) {
name = t;
}

public static void main(String[] args) {
Language cpp  = new Language();
Language java = new Language(“Java”);

cpp.setName(“C++”);

java.getName();
cpp.getName();
}

void setName(String t) {
name = t;
}

void getName() {
System.out.println(“Language name: “ + name);
}
}

Output of program:
Java constructor overloading program output

When we create the CPP object, the default constructor executes. When we create java object, the constructor with argument is called. Method setName is used to set the ‘name’ attribute of language, getName method prints language name.

Java constructor chaining

Constructor chaining occurs when a class inherits another class. In inheritance, a subclass inherits the properties of the superclass. Both the super and subclass may have constructor methods. When we create an object of a subclass, we call its constructor. It initializes subclass attributes; now, we need to invoke the superclass constructor. To achieve this, Java provides a super keyword through which we can pass arguments to the superclass constructor. For more clarity, see constructor chaining example:

class GrandParent {
int a;

GrandParent(int a) {
this.a = a;
}
}

class Parent extends GrandParent {
int b;

Parent(int a, int b) {
super(a);
this.b = b;
}

void show() {
System.out.println(“GrandParent’s a = “ + a);
System.out.println(“Parent’s b      = “ + b);
}
}

class Child {
public static void main(String[] args) {
Parent object = new Parent(8, 9);
object.show();
}
}

Output of program:
Java constructor chaining program example

The constructor method doesn’t specify a return type; it returns an instance of the class itself.

Leave a Reply

Your email address will not be published. Required fields are marked *