SlideShare a Scribd company logo
1 of 13
Presented by,
Siva Shankari Rajan,
CPSV
CONTENTS
What is Constructor
Types of Constructor
Constructor overloading
1
2
3
4
Passing initial values as
arguments
1
Java Constructor
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. It
can be used to set initial
values for object attributes
2
Rules for Java Constructor
The constructor
name must match
the name of the
class
The default
constructor is
ALWAYS as no-arg
constructor
Constructors can
use any access
modifier, including
private
Constructors
must not have a
return type.
3
Difference Between Constructor and Method
Constructor are
used to initialize
the state of
object
Constructor
must not have
return type
Constructor
compil er provide
default
constructor .
Method is expose
the behaviour of
object.
Method must have
return type.
Method compiler
does't provide.
1
2
3
1
2
3
4
Types of Java Constructor
Default
constructor
Parameterized
constructor
Copy
constructor
5
01
02
A constructor that
have no parameter is
known as default
constructor.
Syntax of default
constructor:
<class_name>(){}
Default Constructor
Class Measuring
{
Private double Metre;
Private double kilometer;
Private double centimeter;
Private double Millimeter;
Measuring()
{
Metre=120; kilometer=10.0; centimeter=12.0;
Millimeter=5.0;
}
Void display()
{
System.out.println(“kilometer=“+kilometer);
}
}
Output:
Kilometer=10.0
6
01
02
A constructor that have
parameters is known as
parameterized
constructor.
Parameterized
constructor is used to
provide different values to
the distinct objects
Parameterize
dconstructor
Class Rectangle
{
int length; int breadth;
Rectangle(int len, int bre)
{
length=len;
breadth=bre;
}
}
Class demo
{
Public static void main(String args[])
{
Rectangle r1= new Rectangle(20,10);
System.out.println(“length of rectangle=“
r1.length);
System.out.println(“breadth of
rectangle=“ r1.breadth);
}}
7
01
02
We can create the object
with any initial value to
extract values in the
parameterized
constructor
Parameterized
constructor can pass a
value as:
1. An implicit call
2. An explicit call
Passing initial values
As arguments
Implicit call constructor:
It retrieves the parameterized
constructor even if it is not declared in
the main().
We can create the object with any
initial value to extract values in the
parameterized constructor
Explicit call constructor:
It helps in creating a temporary
instance.
A temporary instance remains in the
memory as long as it is being used in an
expression.
8
01
02
It is used to create a
temporary object of a
class object.
A copy constructor takes
only one argument, which
is of the type as the class.
Copy
constructor
Class Rectangle
{
int length; int breadth;
Rectangle(int len, int bre)
{
length=len;
breadth=bre;
}
Rectangle(Rectangle obj)
{
System.out.println(“copy constructor
invoked”);
length=obj.len;
breadth=obj.bre;
}
int area()
{
return(length*breadth);
}
9
01
02
It is used to create a
temporary object of a
class object.
A copy constructor takes
only one argument, which
is of the type as the class.
Copy
constructor
Class demo
{
Public static void main(String args[])
{
Rectangle r1= new Rectangle(20,10);
Rectangle r2=new Rectangle(r1);
System.out.println(“area of 1st rectangle=“
r1.area());
System.out.println(“area of 2nd rectangle=“
r2.area());
}}
10
01
02
A constructor may be
overloaded, so a single
class may have more
than one constructor, all
of which have the same
name but different
argument lists.
Constructor
overloading
Class overloading
{
int x,
float y;
Overloading()
{x=0;y=0.0;}
Overloading(int a)
{x=a; y=0;}
Overloading(int a, float b)
{x=a; y=b;}
Public static void main(String args[])
{
Overloading ov1=new overloading();
Overloading ov2=new overloading(40);
Overloading ov3=new overloading(10,4.5);
}
}
Constructor in java

More Related Content

What's hot

String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 

What's hot (20)

Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Java program structure
Java program structure Java program structure
Java program structure
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 

Similar to Constructor in java

Similar to Constructor in java (20)

C++
C++C++
C++
 
C++
C++C++
C++
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
5 Constructors and Destructors
5 Constructors and Destructors5 Constructors and Destructors
5 Constructors and Destructors
 
Oops
OopsOops
Oops
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Constructor in java

  • 2. CONTENTS What is Constructor Types of Constructor Constructor overloading 1 2 3 4 Passing initial values as arguments
  • 3. 1 Java Constructor 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. It can be used to set initial values for object attributes
  • 4. 2 Rules for Java Constructor The constructor name must match the name of the class The default constructor is ALWAYS as no-arg constructor Constructors can use any access modifier, including private Constructors must not have a return type.
  • 5. 3 Difference Between Constructor and Method Constructor are used to initialize the state of object Constructor must not have return type Constructor compil er provide default constructor . Method is expose the behaviour of object. Method must have return type. Method compiler does't provide. 1 2 3 1 2 3
  • 6. 4 Types of Java Constructor Default constructor Parameterized constructor Copy constructor
  • 7. 5 01 02 A constructor that have no parameter is known as default constructor. Syntax of default constructor: <class_name>(){} Default Constructor Class Measuring { Private double Metre; Private double kilometer; Private double centimeter; Private double Millimeter; Measuring() { Metre=120; kilometer=10.0; centimeter=12.0; Millimeter=5.0; } Void display() { System.out.println(“kilometer=“+kilometer); } } Output: Kilometer=10.0
  • 8. 6 01 02 A constructor that have parameters is known as parameterized constructor. Parameterized constructor is used to provide different values to the distinct objects Parameterize dconstructor Class Rectangle { int length; int breadth; Rectangle(int len, int bre) { length=len; breadth=bre; } } Class demo { Public static void main(String args[]) { Rectangle r1= new Rectangle(20,10); System.out.println(“length of rectangle=“ r1.length); System.out.println(“breadth of rectangle=“ r1.breadth); }}
  • 9. 7 01 02 We can create the object with any initial value to extract values in the parameterized constructor Parameterized constructor can pass a value as: 1. An implicit call 2. An explicit call Passing initial values As arguments Implicit call constructor: It retrieves the parameterized constructor even if it is not declared in the main(). We can create the object with any initial value to extract values in the parameterized constructor Explicit call constructor: It helps in creating a temporary instance. A temporary instance remains in the memory as long as it is being used in an expression.
  • 10. 8 01 02 It is used to create a temporary object of a class object. A copy constructor takes only one argument, which is of the type as the class. Copy constructor Class Rectangle { int length; int breadth; Rectangle(int len, int bre) { length=len; breadth=bre; } Rectangle(Rectangle obj) { System.out.println(“copy constructor invoked”); length=obj.len; breadth=obj.bre; } int area() { return(length*breadth); }
  • 11. 9 01 02 It is used to create a temporary object of a class object. A copy constructor takes only one argument, which is of the type as the class. Copy constructor Class demo { Public static void main(String args[]) { Rectangle r1= new Rectangle(20,10); Rectangle r2=new Rectangle(r1); System.out.println(“area of 1st rectangle=“ r1.area()); System.out.println(“area of 2nd rectangle=“ r2.area()); }}
  • 12. 10 01 02 A constructor may be overloaded, so a single class may have more than one constructor, all of which have the same name but different argument lists. Constructor overloading Class overloading { int x, float y; Overloading() {x=0;y=0.0;} Overloading(int a) {x=a; y=0;} Overloading(int a, float b) {x=a; y=b;} Public static void main(String args[]) { Overloading ov1=new overloading(); Overloading ov2=new overloading(40); Overloading ov3=new overloading(10,4.5); } }