0% found this document useful (0 votes)
2 views7 pages

Java Practical

The document outlines an experiment on constructors and constructor overloading in Java programming for SY B.Tech students at Sharad Institute of Technology. It defines constructors, illustrates default and parameterized constructors with examples, and explains constructor overloading. The document concludes with exercises for students to develop programs demonstrating these concepts.

Uploaded by

sankett.work07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Practical

The document outlines an experiment on constructors and constructor overloading in Java programming for SY B.Tech students at Sharad Institute of Technology. It defines constructors, illustrates default and parameterized constructors with examples, and explains constructor overloading. The document concludes with exercises for students to develop programs demonstrating these concepts.

Uploaded by

sankett.work07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like