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

VIPS OOPS Unit 2 Constructor

The document provides an overview of Java constructors, explaining their purpose, rules for creation, and types including default, parameterized, and copy constructors. It highlights that constructors initialize an object's state upon creation and must share the same name as the class without a return type. Additionally, it discusses examples and differences between constructors and methods in Java.

Uploaded by

manvipaulemail
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)
5 views

VIPS OOPS Unit 2 Constructor

The document provides an overview of Java constructors, explaining their purpose, rules for creation, and types including default, parameterized, and copy constructors. It highlights that constructors initialize an object's state upon creation and must share the same name as the class without a return type. Additionally, it discusses examples and differences between constructors and methods in Java.

Uploaded by

manvipaulemail
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/ 15

Course :

Paper Code: AIDS 202,


Faculty : Dr. Shivanka
Assistant Professor
VIPS
Constructors
• A constructor initializes an object immediately upon creation.
• It has the same name as the class in which it resides and is
syntactically similar to a method.
• Once defined, the constructor is automatically called immediately
after the object is created, before the new operator completes.
Constructors look a little strange because they have no return type, not
even void. This is because the implicit return type of a class’
constructor is the class type itself.
• It is the constructor’s job to initialize the internal state of an object so
that the code creating an instance will have a fully initialized, usable
object immediately.
Rules for creating Java constructor
• There are two rules defined for the constructor.

• Constructor name must be the same as its class name


• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized.
• A constructor in Java is a special method that is used to initialize
objects. The constructor is called when an object of a class is created.
Types of Java constructors
• There are three types of constructors in Java:

• Default constructor (no-arg constructor):A constructor is called


"Default Constructor" when it doesn't have any parameter.
• Parameterized constructor:The parameterized constructor is used to
provide different values to distinct objects. However, you can provide
the same values also.
• Copy Constructor:There is no copy constructor in Java. However, we
can copy the values from one object to another like copy constructor
in C++.
Default constructor (no-arg constructor)
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any
parameter.

Syntax of default constructor:


<class_name>(){}
Example of default constructor
• In this example, we are creating the no-arg constructor in the
Student class. It will be invoked at the time of object creation. • Note : If there is no constructor
//Java Program to create and call a default constructor in a class, compiler automatically
class Student1{ creates a default constructor.
//creating a default constructor
Student1(){System.out.println("class is created");}
Java default constructor
//main method • Q) What is the purpose of a
public static void main(String args[]){
default constructor?
//calling a default constructor
Student1 b=new Student1(); The default constructor is used to
} } provide the default values to the
Output: object like 0, null, etc., depending
class is created
on the type.
Example of default constructor that displays
the default values
////Let us see another example of default constructor Student2 s1=new Student2();
which displays the default values Student2 s2=new Student2();
class Student2{ //displaying values of the object
int id; s1.display();
String name; s2.display();
//method to display the value of id and name } }
void display(){System.out.println(id+" "+name);} Output:
• 0 null
public static void main(String args[]){ • 0 null
//creating objects • Explanation:In the above class,you are not creating
any constructor so compiler provides you a default
constructor. Here 0 and null values are provided by
default constructor.
Java Parameterized Constructor
• A c o n s t r u c t o r w h i c h h a s a • A constructor that has parameters
specific number of parameters is is known as parameterized
called a parameterized
constructor. c o n s t r u c t o r. I f w e w a n t t o
initialize fields of the class with
• Why use the parameterized our own values, then use a
constructor? parameterized constructor.
• The parameterized constructor is
used to provide different values
to distinct objects. However, you
can provide the same values also.
Example of Parameterized constructor.

import java.io.*; class stud {


class Student { public static void main(String[] args)
// data members of the class.
{
String name;
// This would invoke the parameterized
int id;
constructor.
Student(String name, int id)
Student s = new Student("Gourav", 68);
{
this.name = name;
System.out.println("StudentName :" +
s.name + " and StudentId : " + s.id);
this.id = id;
}
}
} }
Java Program to demonstrate the use of the
parameterized constructor.
class Student3{ //creating objects and passing values
int id; Student3 s1 = new Student3(101,"Pooja");
String name; Student3 s2 = new Student3(201,"Shivam");
//creating a parameterized constructor //calling method to display the values of
Student3(int i,String n){ object

id = i; name = n; } s1.display();

//method to display the values s2.display();

void display(){System.out.println(id+" } }
"+name);} Output:
public static void main(String args[]){ 101 Pooja
201 Shivam
Java Copy Constructor
There is no copy constructor in Java. However, • Unlike other constructors copy
we can copy the values from one object to constructor is passed with another
another like copy constructor in C++. object which copies the data
available from the passed object to
the newly created object.
• There are many ways to copy the values of
one object into another in Java. They are:
• Note: In Java,there is no such inbuilt
copy constructor available like in
• By constructor other programming languages such
as C++, instead we can create our
• By assigning the values of one object into own copy constructor by passing the
another object of the same class to the other
• By clone() method of Object class instance(object) of the class.
//Java program to initialize the values from
one object to another object.

class Student6{ void display(){System.out.println(id+"


int id; String name; "+name);}
//constructor to initialize integer and string public static void main(String args[]){
Student6(int i,String n){ Student6 s1 = new
Student6(111,"Karan");
id = i;
Student6 s2 = new Student6(s1);
name = n; }
s1.display();
//constructor to initialize another object
s2.display();
Student6(Student6 s){
id = s.id; }
name =s.name; } }
Difference between Java Constructor
and Java Method

You might also like