CONSTRUCTOR
BY-
KUSHAGRA KRISHNA
CONTENT
CONSTRUCTOR
TYPES OF CONSTRUCTOR
JAVA DEFAULT CONSTRUCTOR
CREATING A CONSTRUCTOR
JAVA PARAMETERIZED CONSTRUCTOR
CONSTRUCTOR OVERLOADING
MULTIPLE CONSTRUCTORS
JAVA COPY CONSTRUCTOR
DIFFERENCE BETWEEN CONSTRUCTOR AND
METHOD IN JAVA
CONSTRUCTOR
• Constructor is a special method that gets invoked “automatically” at
the time of object creation.
• Constructor is normally used for initializing objects with default
values
unless different values are supplied.
• Constructor has the same name as the class name.
• Constructor cannot return values.
• A class can have more than one constructor as long as they have
different signature.
JAVA DEFAULT CONSTRUCTOR
A constructor
is called "Default
Constructor" when it doesn't have any
parameter.
EXAMPLE OF DEFAULT CONSTRUCTOR
class Bike1{
//creating a default constructor
Bike1()
{System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
CREATING A CONSTRUCTOR:
EXAMPLE
// Create a Main class
public class Main {
int x; // Create a class attribute
// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class attribute x
}
public static void main(String[] args) {
Main myObj = new Main(); // Create an object of class
Main (This will call the constructor)
System.out.println(myObj.x);
} 7
}
8
JAVA PARAMETERIZED CONSTRUCTOR
A constructor that have parameters is known as
parameterized constructor.
Why use parameterized
constructor?
Parameterized constructor is used to provide different
values to the distinct objects.
9
Parameterized constructor EXAMPLE
public class Main {
int x;
public Main(int y) {
x = y;
}
public static void main(String[] args)
{
Main myObj = new Main(5);
System.out.println(myObj.x);
}
}
CONSTRUCTOR
OVERLOADING
Constructor overloading is a technique in Java in
which a class can have any number of
constructors that differ in parameter lists. The
compiler differentiates these constructors by
taking into account the number of parameters in
the list and their type.
11
CONSTRUCTOR OVERLOADING EXAMPLE
MULTIPLE CONSTRUCTORS
Sometimes want to initialize in a number
of different ways, depending on
circumstance.
This can be supported by having multiple
constructors having different input
arguments.
13
MULTIPLE CONSTRUCTORS
public class Circle {
public double x,y,r; //instance variables
// Constructors
public Circle(double centreX, double cenreY, double radius)
{ x = centreX; y = centreY; r = radius;
}
public Circle(double radius) { x=0; y=0; r = radius; }
public Circle() { x=0; y=0; r=1.0; }
//Methods to return circumference and area
public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}
14
JAVA COPY CONSTRUCTOR
There is no copy constructor in java. But, we can copy the
values of one object to another like copy constructor in
C++.
There are many ways to copy the values of one object
into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
15
DIFFERENCE BETWEEN CONSTRUCTOR AND
METHOD IN JAVA
16
THANK YOU