Sharad Institute Of Technology College of Engineering Yadrav(An Autonomous Institute)
Course ;Java Programming Class:SY [Link]
Experiment No:4
Title:
Program on Constructor and Constructor
Overloading
Aim: Able to build program on concept Constructor
Theory
What is Constructor?
Definition:
A constructor is a special member function in a class that is automatically invoked when an
object of the class is created. It is primarily used to initialize the object.
Syntax:
class ClassName
{ public:
ClassName() {
// Constructor Body
}
}
Example:
class Car {
String brand;
int year;
// Default Constructor
Car() {
brand =
"BMW"; year =
2021;
}
void display() {
[Link]("Brand: " + brand + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car(); // Default constructor is called
[Link]();
}
}
Output:
Brand: BMW ,Year: 2021
Sharad Institute Of Technology College of Engineering Yadrav(An Autonomous Institute)
Course ;Java Programming Class:SY [Link]
Rules:
Sharad Institute Of Technology College of Engineering Yadrav(An Autonomous Institute)
Course ;Java Programming Class:SY [Link]
The name of the constructor must be the same as the class name.
Constructors cannot have a return type (not even void).
They are automatically called when an object is created.
A class can have multiple constructors (constructor overloading).
Illustrate the Types of Constructors:
Default Constructor:
A constructor that takes no parameters and is used to initialize objects with default values.
Example:
Example:
class Car {
String brand;
int year;
// Default Constructor
Car() {
brand = "BMW";
year = 2021;
}
void display() {
[Link]("Brand: " + brand + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car(); // Default constructor is called
[Link]();
}
}
Output:
Brand: BMW ,Year: 2021
Parameterized Constructor:
A constructor that takes arguments to initialize objects with specific values.
Example:
class Car
{
Sharad Institute Of Technology College of Engineering Yadrav(An Autonomous Institute)
Course ;Java Programming Class:SY [Link]
String brand;
int year;
// Parameterized Constructor
Car(String b, int y) {
brand = b;
year = y;
}
void display() {
[Link]("Brand: " + brand + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2021); // Parameterized constructor is called
[Link]();
}
}
Output. :
Brand: Toyota, Year: 2021
Illustrate Constructor Overloading:
Definition:
Constructor overloading is when a class has multiple constructors with the same name but
different parameter lists.
Example:
class Car {
String brand;
int year;
// Default Constructor
Car() {
brand = "Unknown";
year = 0;
}
// Parameterized Constructor with 1 argument
Car(String b) {
brand =
b; year =
0;
}
// Parameterized Constructor with 2 arguments
Car(String b, int y)
{ brand = b;
year = y;
}
void display() {
[Link]("Brand: " + brand + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car(); // Default constructor
Car car2 = new Car("Honda"); // Constructor with 1 argument
Car car3 = new Car("Ford", 2022); // Constructor with 2 arguments
[Link]();
[Link]();
[Link]();
}
}
Output:
Brand: Unknown, Year: 0
Brand: Honda, Year: 0
Brand: Ford, Year: 2022
Conclusion:
In this experiment we learnt program on constructor and constructor overloading
exercise:
1. Develop a program for default constructor
2. Develop a program for Parameterized constructor
3. Develop a program for constructor overloading.