SlideShare a Scribd company logo
2
Most read
6
Most read
13
Most read
Constructor
Compiled by:
Vinod Kumar(Asst. prof.)
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 (i.e., different input arguments syntax).
3
Defining a Constructor: Example
public class Counter {
int CounterIndex;
// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or access counter
public void increase()
{
CounterIndex = CounterIndex + 1;
}
public void decrease()
{
CounterIndex = CounterIndex - 1;
}
int getCounterIndex()
{
return CounterIndex;
}
}
4
Defining a Constructor: Example
public class Counter {
int CounterIndex;
// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or access counter
public void increase()
{
CounterIndex = CounterIndex + 1;
}
public void decrease()
{
CounterIndex = CounterIndex - 1;
}
int getCounterIndex()
{
return CounterIndex;
}
}
5
Trace counter value at each
statement and What is the output ?
class MyClass {
public static void main(String args[])
{
Counter counter1 = new Counter();
counter1.increase();
int a = counter1.getCounterIndex();
counter1.increase();
int b = counter1.getCounterIndex();
if ( a > b )
counter1.increase();
else
counter1.decrease();
System.out.println(counter1.getCounterIndex());
}
}
Java parameterized constructor
A constructor that have parameters is known as parameterized
constructor.
6
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the
distinct objects.
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.
7
8
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.
9
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; }
}
10
Initializing with constructors
public class TestCircles {
public static void main(String args[]){
Circle circleA = new Circle( 10.0, 12.0, 20.0);
Circle circleB = new Circle(10.0);
Circle circleC = new Circle();
}
}
circleA = new Circle(10, 12, 20) circleB = new Circle(10)
Centre = (0,0)
Radius=10
circleC = new Circle()
Centre = (0,0)
Radius = 1
Centre = (10,12)
Radius = 20
Java Copy Constructor
11
 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
Copying values without constructor
12
 We can copy the values of one object into another by
assigning the objects values to another object. In this case,
there is no need to create the constructor.
Difference between constructor and method in
java
13

More Related Content

What's hot (20)

PPTX
Pointer in c
lavanya marichamy
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
Structures in c language
tanmaymodi4
 
PPTX
class and objects
Payel Guria
 
PPTX
Characteristics of OOPS
abhishek kumar
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Operators in java
Then Murugeshwari
 
PDF
Files in java
Muthukumaran Subramanian
 
PDF
C++ OOPS Concept
Boopathi K
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PPTX
constructors in java ppt
kunal kishore
 
PPTX
Inheritance in java
RahulAnanda1
 
Pointer in c
lavanya marichamy
 
Pointers in c++
sai tarlekar
 
classes and objects in C++
HalaiHansaika
 
07. Virtual Functions
Haresh Jaiswal
 
Structures in c language
tanmaymodi4
 
class and objects
Payel Guria
 
Characteristics of OOPS
abhishek kumar
 
Interface in java
PhD Research Scholar
 
Operators in java
Then Murugeshwari
 
C++ OOPS Concept
Boopathi K
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Function overloading(c++)
Ritika Sharma
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Type casting in java
Farooq Baloch
 
Files in c++
Selvin Josy Bai Somu
 
constructors in java ppt
kunal kishore
 
Inheritance in java
RahulAnanda1
 

Viewers also liked (20)

PPTX
Constructors & destructors
ForwardBlog Enewzletter
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PPTX
Constructor and destructor in c++
Learn By Watch
 
PPT
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Class object method constructors in java
Raja Sekhar
 
PDF
Constructor and Destructor
Kamal Acharya
 
PPT
Java lec constructors
Jan Niño Acierto
 
PPTX
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Constructor overloading in C++
Learn By Watch
 
PPT
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
PPT
Object Oriented Programming Concepts
thinkphp
 
PPT
Java lec class, objects and constructors
Jan Niño Acierto
 
PPT
Constructor and Destructor PPT
Shubham Mondal
 
PPT
Constructor
poonamchopra7975
 
PPTX
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
Inheritance
Sapna Sharma
 
PPT
Oops ppt
abhayjuneja
 
PPTX
Parameterized Constructor
Mukesh Pathak
 
Constructors & destructors
ForwardBlog Enewzletter
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor and destructor in c++
Learn By Watch
 
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Class object method constructors in java
Raja Sekhar
 
Constructor and Destructor
Kamal Acharya
 
Java lec constructors
Jan Niño Acierto
 
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
Constructor overloading in C++
Learn By Watch
 
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Object Oriented Programming Concepts
thinkphp
 
Java lec class, objects and constructors
Jan Niño Acierto
 
Constructor and Destructor PPT
Shubham Mondal
 
Constructor
poonamchopra7975
 
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
Classes, objects in JAVA
Abhilash Nair
 
Inheritance
Sapna Sharma
 
Oops ppt
abhayjuneja
 
Parameterized Constructor
Mukesh Pathak
 
Ad

Similar to Constructor ppt (20)

PPTX
Concept of constructors
kunj desai
 
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
PPTX
constructors.pptx
Epsiba1
 
PPTX
Java Constructor
MujtabaNawaz4
 
PPTX
Week 3-LectureA Object Oriented Programmings.pptx
FarhanGhafoor7
 
PDF
Constructors in Java (2).pdf
kumari36
 
PPT
Constructors.16
myrajendra
 
PPTX
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
PPTX
Lecture 5
talha ijaz
 
PPTX
constactor overloading.pptx Tanisha patel
jenish07102011
 
PPT
JAVA CONCEPTS
Shivam Singh
 
PDF
Constructors In Java – Unveiling Object Creation
Geekster
 
PPTX
unit 2 java.pptx
AshokKumar587867
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PPTX
Getters_And_Setters.pptx
Kavindu Sachinthe
 
PDF
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
kakarthik685
 
PDF
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
tabbu23
 
PPT
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
PPTX
object oriented programming CONSTRUCTORS.pptx
MattFlordeliza1
 
PPTX
Java
nirbhayverma8
 
Concept of constructors
kunj desai
 
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
constructors.pptx
Epsiba1
 
Java Constructor
MujtabaNawaz4
 
Week 3-LectureA Object Oriented Programmings.pptx
FarhanGhafoor7
 
Constructors in Java (2).pdf
kumari36
 
Constructors.16
myrajendra
 
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
Lecture 5
talha ijaz
 
constactor overloading.pptx Tanisha patel
jenish07102011
 
JAVA CONCEPTS
Shivam Singh
 
Constructors In Java – Unveiling Object Creation
Geekster
 
unit 2 java.pptx
AshokKumar587867
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Getters_And_Setters.pptx
Kavindu Sachinthe
 
Java-Module 3-PPT-TPS.pdf.Java-Module 3-PPT-TPS.pdf
kakarthik685
 
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
tabbu23
 
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
object oriented programming CONSTRUCTORS.pptx
MattFlordeliza1
 
Ad

Recently uploaded (20)

PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
July Patch Tuesday
Ivanti
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
July Patch Tuesday
Ivanti
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 

Constructor ppt

  • 2. 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 (i.e., different input arguments syntax).
  • 3. 3 Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() { CounterIndex = 0; } //Methods to update or access counter public void increase() { CounterIndex = CounterIndex + 1; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; } }
  • 4. 4 Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() { CounterIndex = 0; } //Methods to update or access counter public void increase() { CounterIndex = CounterIndex + 1; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; } }
  • 5. 5 Trace counter value at each statement and What is the output ? class MyClass { public static void main(String args[]) { Counter counter1 = new Counter(); counter1.increase(); int a = counter1.getCounterIndex(); counter1.increase(); int b = counter1.getCounterIndex(); if ( a > b ) counter1.increase(); else counter1.decrease(); System.out.println(counter1.getCounterIndex()); } }
  • 6. Java parameterized constructor A constructor that have parameters is known as parameterized constructor. 6 Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects.
  • 7. 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. 7
  • 8. 8 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.
  • 9. 9 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; } }
  • 10. 10 Initializing with constructors public class TestCircles { public static void main(String args[]){ Circle circleA = new Circle( 10.0, 12.0, 20.0); Circle circleB = new Circle(10.0); Circle circleC = new Circle(); } } circleA = new Circle(10, 12, 20) circleB = new Circle(10) Centre = (0,0) Radius=10 circleC = new Circle() Centre = (0,0) Radius = 1 Centre = (10,12) Radius = 20
  • 11. Java Copy Constructor 11  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
  • 12. Copying values without constructor 12  We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
  • 13. Difference between constructor and method in java 13