Explain different types of constructors with suitable example.
Types of Constructors in Java
There are 2 types of constructors in Java based on parameters:
1. Non-parameterized / No arguments Constructor
When we do not pass arguments in the constructor, that constructor is known as a non- parameterized or no-argument constructor. When the programmer does not define any constructor in the Java program, the Java compiler itself adds a constructor, known as the default constructor, which provides default values to the object like 0, null, etc. The default constructor is no special type of constructor but falls in the category of no arguments constructor.
Example (Non-Parameterized Constructor)
public DemoProgram() // class
{
DemoProgram() // constructor with no arguments
{
System.out.println(“Hello this is just an example of no-arg constructor”);
}
public static void main(String[] args)
{
new DermoProgram();
}
}
Output: Non-Parameterized Constructor
As in the above example, for the constructor DemoProgram(), there are no arguments passed, only the message is printed, and hence it is known as the No-argument constructor.
2. Parameterized Constructor
Parameterized constructors are those constructors in which we pass the arguments or parameters. In this constructor, values are passed at the time of object creation.
Example (Parameterized Constructors)
public class Animal()
{
int legs;
String sound;
Animal(int legs, String sound) // parameterized constructor
{
this.legs = legs; // values with get initialize of what is passed while object crea-this.sound = sound; // tion, i.e. (4, “bow bow”)
}
void display() // method to display the values
{
System.out.println(“Legs are ”+legs+“Sound is ”+sound);
}
}
class AnimalPlanet()
{
Public static void main(String[] args)
{
Animal an = new Animal(4, “bow bow”);
an.display();
}
}
Output: Parameterized Constructor
In the above example, value 4 is assigned to the legs, and the string “bow bow” is assigned to the sound in constructor Animal. So when the method display is called, both the values get printed in the output.
OR,
1)Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
<class_name>(){}
Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output: Bike is created
2) Java Parameterized Constructor
- A constructor which has a specific number of parameters is called a parameterized constructor.
- The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
//Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output:111 Karan
222 Aryan
OR,
Types of Constructors
There are three types of constructors: Default, No-arg constructor, and Parameterized.
1) Default, constructor
If you do not implement any constructor in your class, the Java compiler inserts a default constructor into your code on your behalf. This constructor is known as the default constructor. You would not find it in your source code(the java file) as it would be inserted into the code during compilation and exists in the .class file. This process is shown in the diagram below:
If you implement any constructor then you no longer receive a default constructor from Java compiler.
2) no-arg constructor:
- Constructor with no arguments is known as no-arg constructor. The signature is the same as the default constructor, however body can have any code unlike default constructor where the body of the constructor is empty.
- Although you may see some people claim that that default and no-arg constructor is same in fact they are not, even if you write public Demo() { } in your class Demo it cannot be called default constructor since you have written the code of it.
- Example: no-arg constructor
class Demo
{
public Demo()
{
System.out.println("This is a no argument constructor");
}
public static void main(String args[]) {
new Demo();
}
}
Output: This is a no-argument constructor
3) Parameterized constructor
Constructor with arguments(or you can say parameters) is known as Parameterized constructor.
Example: parameterized constructor
In this example we have a parameterized constructor with two parameters id and name. While creating the objects obj1 and obj2 I have passed two arguments so that this constructor gets invoked after creation of obj1 and obj2.
public class Employee {
int empId;
String empName;
//parameterized constructor with two parameters
Employee(int id, String name){
this.empId = id;
this.empName = name;
}
void info(){
System.out.println("Id: "+empId+" Name: "+empName);
}
public static void main(String args[]){
Employee obj1 = new Employee(10245,"Chaitanya");
Employee obj2 = new Employee(92232,"Negan");
obj1.info();
obj2.info();
}
}
Output:
Id: 10245 Name: Chaitanya
Id: 92232 Name: Negan
Comments
Post a Comment