0% found this document useful (0 votes)
5 views

Constructor in Java

The document explains the concept of constructors in Java, including default and parameterized constructors, as well as constructor overloading. It provides examples to illustrate how constructors are used to initialize objects and how values can be copied between objects. Additionally, it discusses the absence of a built-in copy constructor in Java and alternative methods for copying object values.

Uploaded by

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

Constructor in Java

The document explains the concept of constructors in Java, including default and parameterized constructors, as well as constructor overloading. It provides examples to illustrate how constructors are used to initialize objects and how values can be copied between objects. Additionally, it discusses the absence of a built-in copy constructor in Java and alternative methods for copying object values.

Uploaded by

Youllneverknowme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Constructor:

• To initialize the object


• 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.

Rules for creating java constructor:


• Constructor name must be same as its class name
• Constructor must have no explicit return type
Default Constructor:

A constructor that have no parameter is known as default


constructor.
class Bike
{
Bike()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike b=new Bike();
}
} Output: Bike is
created
Example of default constructor:

• If there is no constructor in a class, compiler automatically


creates a default constructor

class Student3
{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display(); Output:
} } 0 null
0 null
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.
Example of parameterized constructor:
class Student4{
int id;
String name;
Student4(int i, String n)
{
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
Output:
s1.display(); 111 Karan
s2.display(); 222 Aryan
} }
Constructor Overloading:
class Student5{
int id;
{
String name;
int age; Student5 s1 = new Student5(111,"Ka
Student5(int i,String n){ ran");
id = i;
Student5 s2 = new Student5(222,"Ar
name = n; yan",25);
} s1.display();
Student5(int i,String n,int a){ s2.display();
id = i; }
name = n; }
age=a; Output:
} 111 Karan
public static void main(String 222 Aryan 25
args[])
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
Copy the values of one object into another using java constructor :

class Student6{
int id;
String name;
Student6(int i,String n){
id = i;
name = n;
}

Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
Output:
111 Karan
public static void main(String args[]){
Student6 s1 = new Student6(111,"Karan");
111 Karan
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
Copying by assigning the values of one object into another :

class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


Student7 s1 = new Student7(111,"Karan"); Output:
Student7 s2 = new Student7(); 111 Karan
s2.id=s1.id; 111 Karan
s2.name=s1.name;
s1.display();
s2.display();
}
}

You might also like