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

OOP - EXPT4 - Java Program Using Constructor

Uploaded by

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

OOP - EXPT4 - Java Program Using Constructor

Uploaded by

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

AMRUTVAHINI COLLEGE OF ENGINEERING, SANGAMNER

Signal & System And Object Oriented Programming Lab


Experiment No.: 04 Java Program Using Constructor Page 1/4

TITLE: Java Program Using Constructor

AIM: To write a Java program using a constructor to create a Bank Account class with
functionalities such as deposit, withdraw, and balance checking.

THEORY:

A constructor is a special method in Java that is automatically called when an object is created.
It is used to initialize object values.

Characteristics of Constructors:

Same name as the class

No return type (not even void)

Automatically invoked at object creation

Used to initialize variables

Types of Constructors:

[Link] Constructor – No parameters

[Link] Constructor – Accepts values as parameters

[Link] Constructor – Used to copy values of another object (not built-in, defined manually)

1. Default Constructor:

A default constructor is a constructor with no [Link] initializes objects with default or fixed
initial values.

Syntax: ClassName()

{ } Example:

Student() { name = "ABC"; }


AMRUTVAHINI COLLEGE OF ENGINEERING, SANGAMNER

Signal & System And Object Oriented Programming Lab


Experiment No.: 04 Java Program Using Constructor Page 2/4

Student s1 = new Student();

2. Parameterized Constructor:

A parameterized constructor accepts values from the user and initializes the [Link] allows
different objects to have different initial values.

Syntax:

ClassName(type x) { }

Example:

Student(String n){ name = n; } Student

s1 = new Student("Amit");

3. Copy Constructor:

A copy constructor creates a new object by copying values from another object.
Java does not provide a built-in copy constructor, so it is defined manually.

Syntax:

ClassName(ClassName obj) { }

Example:

Student(Student s){ [Link] = [Link]; }

Student s2 = new Student(s1);

ADVANTAGES OF USING CONSTRUCTORS:

Automatically initialize objects


AMRUTVAHINI COLLEGE OF ENGINEERING, SANGAMNER

Signal & System And Object Oriented Programming Lab


Experiment No.: 04 Java Program Using Constructor Page 3/4

Reduces code duplication

Ensures object starts in valid state Improves

readability and maintainability

DISADVANTAGES:

If parameters are many, constructor becomes lengthy

Constructor overloading increases complexity Bank

Account Program :

A Bank Account class usually contains:

Account Number

Account Holder Name

Balance

Functionalities Required:

[Link](): Add amount to balance

[Link](): Deduct amount (only if balance is sufficient)

[Link](): Display current balance Use

of Constructor:

We will use a constructor to initialize:

Account holder name

Account number

Initial balance
AMRUTVAHINI COLLEGE OF ENGINEERING, SANGAMNER

Signal & System And Object Oriented Programming Lab


Experiment No.: 04 Java Program Using Constructor Page 4/4

This demonstrates object initialization using constructors.

CONCLUSION:

QUESTIONS:·

[Link] is a constructor in Java?

[Link] do constructors not have a return type?

[Link] is the difference between constructor and method?

[Link] is a parameterized constructor?

[Link] happens if you do not define any constructor?

REFERENCES:

[1] E. Balgurusamy, “Object Oriented Programming with C++”, 7th Edition, Tata McGraw Hill,
New Delhi, 2001, ch-4, pp.79-81.
[2]Herbert Schildt, Java: The Complete Reference, McGraw-Hill.

You might also like