0% found this document useful (0 votes)
8 views

Constructor in Java

Constructor in java

Uploaded by

Sanket Bodade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Constructor in Java

Constructor in java

Uploaded by

Sanket Bodade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim : Develop a program to study and implement concept of Constructor in Java.

A Constructor is a block of codes similar to the method. It is called when an instance of the
class is created. At the time of calling the constructor, memory for the object is allocated in the
memory. It is a special type of method that is used to initialize the object. Every time an object
is created using the new() keyword, at least one constructor is called.

How Java Constructors are Different From Java Methods?


 Constructors must have the same name as the class within which it is defined it is not
necessary for the method in Java.
 Constructors do not return any type while method(s) have the return type or void if does
not return any value.
 Constructors are called only once at the time of Object creation while method(s) can be
called any number of times.

Types of Constructors in Java


 Default Constructor
 Parameterized Constructor
 Copy Constructor

1. Default Constructor:

A constructor that has no parameters is known as default the constructor. A default constructor
is invisible. And if we write a constructor with no arguments, the compiler does not create a
default constructor. It is taken out. It is being overloaded and called a parameterized
constructor. The default constructor changed into the parameterized constructor. But
Parameterized constructor can’t change the default constructor. The default constructor can be
implicit or explicit. If we don’t define explicitly, we get an implicit default constructor. If we
manually write a constructor, the implicit one is overridden.

2. Parameterized Constructor in Java

Constructor that has parameters is known as parameterized constructor. If we want to initialize


fields of the class with our own values, then use a parameterized constructor.
3. Copy Constructor in Java

Unlike other constructors copy constructor is passed with another object which copies the data
available from the passed object to the newly created object.

public class Person // Define a class named Person

private String name; // Data members (attributes) of the class

private int age;

public Person(String name, int age) // Constructor of the class

this.name = name;

this.age = age;

public void displayInfo() // Method to display the person's details

System.out.println("Name: " + name);

System.out.println("Age: " + age);

public static void main(String[] args)

Person person1 = new Person("Alice", 30); // Create a new Person object using the
constructor
// Call the displayInfo method to print the details of the person

person1.displayInfo();

}
}

Output

Name: Alice
Age: 30

You might also like