Lecture – 6
Constructor and Constructor Overloading
Afsara Tasneem Misha
Lecturer
Department of CSE
Daffodil International University
Contents
• Constructor in Java
• Constructor Overloading
2
Topic – 1: Constructor in Java
3
Introduction to Constructor
• A constructor in Java is a special method
• used to initialize objects.
• constructor name must match the class name
• it cannot have a return type (not even void).
• Also note that the constructor is called when the object is created.
• All classes have constructors by default: if you do not create a class constructor yourself,
Java creates one for you and initiate the variables with default value such as 0 or NULL.
4
Compiler creates constructor for you
5
Example – 1
6
Example – 2
public class MyClass {
int x;
public MyClass() {
x = 5;
}
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
7
Example
Example -–33
public class Demo
{
public Demo()
{
System.out.println("This is a no argument constructor");
}
public static void main(String args[]) {
Demo demo1 = new Demo();
}
}
8
Types of Java constructors
There are two types of constructors in Java:
1.Default constructor (no-arg constructor)
2.Parameterized constructor
9
1. Default constructor (no-arg constructor)
10
Example : Default constructor (no-arg constructor)
public class Box1 {
int height;
int width;
public Box1()
{
System.out.println("Box Created.");
this.height = 10;
this.width = 20;
}
public static void main(String[] args) {
Box1 box1 = new Box1();
System.out.println("The height of box1 = "+box1.height);
System.out.println("The width of box1 = "+box1.width);
Box1 box2 = new Box1();
System.out.println("\nThe height of box2 = "+box2.height);
System.out.println("The width of box2 = "+box2.width);
} 11
2. Parameterized constructor
public class MyClass {
int x;
public MyClass ( int y) {
x = y;
}
public static void main(String[] args) {
MyClass myObj = new MyClass(50);
System.out.println(myObj.x);
}
}
12
Example - 1 : Parameterized constructor
public class Box2 {
int height;
int width;
public Box2(int a, int b) {
System.out.println("\nBox Created.");
this.height = a;
this.width = b;
}
public static void main(String[] args) {
Box2 box1 = new Box2(10, 20);
System.out.println("The height of box1 = " + box1.height);
System.out.println("The width of box1 = " + box1.width);
Box2 box2 = new Box2(15, 25);
System.out.println("The height of box2 = " + box2.height);
System.out.println("The width of box2 = " + box2.width);
}
} 13
Example - 2 :
Parameterized
constructor
14
Example - 2 :
Example: 1
Parameterized
constructor
15
Topic – 2: Constructor Overloading in Java
16
Constructor Overloading
• Constructor overloading is a
concept of having more than one
constructors with different
parameters list, in such a way so
that each constructor performs a
different task.
17
public class Box {
private int height , width;
Example - 1 :
constructor public Box() {
Overloading height = 10;
width = 15;
}
public Box (int height, int width) {
this.height = height;
this.width = width;
}
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box(20,30);
Box b3 = new Box(50,60);
Box b4 = new Box();
System.out.println("For Box-1: Height = " + b1.heigh+ “Width = “ +b1.width);
System.out.println("For Box-2: Height = " + b2.heigh+ “Width = “ +b2.width);
System.out.println("For Box-3: Height = " + b3.heigh+ “Width = “ +b3.width);
System.out.println("For Box-4: Height = " + b4.heigh+ “Width = “ +b4.width);
}
}
18
Example - 2 :
constructor
Overloading
19
Difference between constructor and method in Java
Java Constructor Java Method
A constructor is used to initialize the A method is used to expose the
state of an object. behavior of an object.
A constructor must not have a A method must have a return type.
return type.
The constructor is invoked The method is invoked explicitly.
implicitly.
The Java compiler provides a The method is not provided by the
default constructor if you don't have compiler in any case.
any constructor in a class.
The constructor name must be The method name may or may not
same as the class name. be same as the class name.
20
Some helpful Links
• https://www.w3schools.com/java/java_constructors.asp
• https://beginnersbook.com/2013/03/constructors-in-java/
• https://www.javatpoint.com/java-constructor
21
Thank you!
22