0% found this document useful (0 votes)
15 views3 pages

JPL Expt-4 Minor

hijjbjjskwhsiddhisshis sishsisjsis

Uploaded by

chudjisarvesh20
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)
15 views3 pages

JPL Expt-4 Minor

hijjbjjskwhsiddhisshis sishsisjsis

Uploaded by

chudjisarvesh20
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
You are on page 1/ 3

Subject: Java Lab Class: SE-IT

Semester: III A.Y. 2024-2025

Experiment No. 4

➢ Aim : Write a Java program to demonstrate different constructors in java along


with constructor chaining.
➢ Software used : jdk 1.6.0

➢ Objectives : To understand use of constructor in java to initialize object of a class.and to

learn how to maintain initialization of multiple constructors from a single location after
having multiple constructors using constructor chaining.

➢ Prerequisites : Students should know how to define java class with importance of class
object and its formation while program execution.

➢ Theory :
- Constructor in java is a special type of method that is used to initialize the object.

- Java constructor is invoked at the time of object creation. It constructs the values i.e.
provides data for the object that is why it is known as constructor.
- There are basically two rules defined for the constructor.

1. Constructor name must be same as its class name


2. Constructor must have no explicit return type

– Difference between constructor and method in java

Java Constructor Java Method

Use to initialize state of an object Used to expose behaviour of an object


Constructor must not have return type Methods must have return type
Constructor is invoked impliciltely Method is invoked explicitely
Java compliler provides default constructor if
Method is not provided by compiler in any case
we dont have constructor in program
Constrcutor name must be same as the class Method name may or may not be same as class
name name
- Types of constructors :

Types of Constructors

1] Default Constructor :

◦ The java class without having any constructor implemented within it is called as default
constructor .
◦ If you do not implement any constructor in your class, Java compiler inserts a default
constructor into your code on your behalf.
◦ 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 .class file. This process is shown in the diagram below:

2] No-Argument Constructor :

◦ Constructor with no arguments is known as no-arg constructor.


◦ The signature is same as 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 default and no-arg constructor is same but 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.

2] Parameterized constructor
- A constructor that have parameters is known as parameterized constructor.
- Parameterized constructor is used to provide different values to the distinct objects.
- If we want to initialize fields of the class with your own values, then use parameterized constructor.
- We can have any number of parameters in the constructor.

❖ Constructor Chaining :

◦ Calling a constructor from the another constructor of same class is known as Constructor
chaining.
◦ The real purpose of Constructor Chaining is that you can pass parameters through a
bunch of different constructors, but only have the initialization done in a single place.
◦ This allows you to maintain your initializations from a single location, while providing
multiple constructors to the user.

◦ If we don’t chain, and two different constructors require a specific parameter, you will
have to initialize that parameter twice, and when the initialization changes, you’ll have to
change it in every constructor, instead of just the one.

◦ this() and super() are used to call constructors explicitly. Where, using this() you can call
the current class’s constructor and using super() you can call the constructor of the super
class.

◦ From normal (default) constructor you can call the parameterized constructors of the
same class using this() and, from the sub class you can call the constructor of the super
class using super()

Conclusion: Summarize what you have learned from the lab.

You might also like