SlideShare a Scribd company logo
3
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

PPT
C by balaguruswami - e.balagurusamy
PPTX
Constructor in java
PPTX
Organic Rice
PPTX
Constructor in java
PPTX
JAVA AWT
PPT
Method overriding
PPTX
Structure in C
DOCX
CBSE Class 12 Computer practical Python Programs and MYSQL
C by balaguruswami - e.balagurusamy
Constructor in java
Organic Rice
Constructor in java
JAVA AWT
Method overriding
Structure in C
CBSE Class 12 Computer practical Python Programs and MYSQL

What's hot (20)

PPTX
Java constructors
PPTX
Constructor and Types of Constructors
PPTX
Data types in java
PPTX
Control structures in java
PPTX
Java packages
PPTX
Packages in java
PPTX
Super keyword in java
PPTX
Java(Polymorphism)
PPTX
Control Statements in Java
PPTX
Interface in java
PPTX
Access Modifier.pptx
PPTX
Inheritance in c++
PPTX
Inheritance in java
PPTX
Type casting in java
PDF
Function overloading ppt
PPTX
Applets in java
PPTX
Method overloading
PPTX
Operators in java
PPTX
classes and objects in C++
PPTX
Java swing
Java constructors
Constructor and Types of Constructors
Data types in java
Control structures in java
Java packages
Packages in java
Super keyword in java
Java(Polymorphism)
Control Statements in Java
Interface in java
Access Modifier.pptx
Inheritance in c++
Inheritance in java
Type casting in java
Function overloading ppt
Applets in java
Method overloading
Operators in java
classes and objects in C++
Java swing
Ad

Viewers also liked (20)

PPTX
Constructors & destructors
PDF
Constructors and Destructors
PPTX
Constructor in java
PPT
constructor and destructor-object oriented programming
PPTX
Constructor and destructor in c++
PPT
Constructor & Destructor
PPTX
Class object method constructors in java
PDF
Constructor and Destructor
PPT
Java lec constructors
PPTX
Methods and constructors in java
PPTX
Constructor overloading in C++
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPT
Object Oriented Programming Concepts
PPT
Java lec class, objects and constructors
PPT
Constructor and Destructor PPT
PPT
Constructor
PPTX
Method overloading and constructor overloading in java
PPTX
Classes, objects in JAVA
PPTX
Inheritance
PPT
Oops ppt
Constructors & destructors
Constructors and Destructors
Constructor in java
constructor and destructor-object oriented programming
Constructor and destructor in c++
Constructor & Destructor
Class object method constructors in java
Constructor and Destructor
Java lec constructors
Methods and constructors in java
Constructor overloading in C++
C++: Constructor, Copy Constructor and Assignment operator
Object Oriented Programming Concepts
Java lec class, objects and constructors
Constructor and Destructor PPT
Constructor
Method overloading and constructor overloading in java
Classes, objects in JAVA
Inheritance
Oops ppt
Ad

Similar to Constructor ppt (20)

PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPTX
Concept of constructors
PPTX
Lab4-Software-Construction-BSSE5.pptx ppt
PPTX
constactor overloading.pptx Tanisha patel
PPTX
Constructors and Destructors
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPT
Constructors and destructors in C++
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPT
Refactoring - improving the smell of your code
PPTX
constructors.pptx
PPT
Review constdestr
PDF
Lecture-03 _Java Classes_from FAST-NUCES
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PDF
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
PPTX
05 Object Oriented Concept Presentation.pptx
PDF
Chapter19 constructor-and-destructor
PPTX
Introductions to Constructors.pptx
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PDF
Constructor and Destructor.pdf
Unit 1 Part - 3 constructor Overloading Static.ppt
Concept of constructors
Lab4-Software-Construction-BSSE5.pptx ppt
constactor overloading.pptx Tanisha patel
Constructors and Destructors
Constructors & Destructors [Compatibility Mode].pdf
Constructors and destructors in C++
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Refactoring - improving the smell of your code
constructors.pptx
Review constdestr
Lecture-03 _Java Classes_from FAST-NUCES
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
05 Object Oriented Concept Presentation.pptx
Chapter19 constructor-and-destructor
Introductions to Constructors.pptx
Chapter 2 OOP using C++ (Introduction).pptx
Constructor and Destructor.pdf

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Getting Started with Data Integration: FME Form 101
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Tartificialntelligence_presentation.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Mushroom cultivation and it's methods.pdf
Teaching material agriculture food technology
Getting Started with Data Integration: FME Form 101
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
Tartificialntelligence_presentation.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Heart disease approach using modified random forest and particle swarm optimi...
A comparative analysis of optical character recognition models for extracting...
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Reach Out and Touch Someone: Haptics and Empathic Computing
SOPHOS-XG Firewall Administrator PPT.pptx
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mushroom cultivation and it's methods.pdf

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