SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
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

PPTX
I/O Streams
PPTX
Constructor ppt
PPTX
Constructor in java
PPTX
Constructor in java
PPTX
Java constructors
PPTX
Java Methods
PPTX
Constructor overloading & method overloading
PPT
Data types
I/O Streams
Constructor ppt
Constructor in java
Constructor in java
Java constructors
Java Methods
Constructor overloading & method overloading
Data types

What's hot (20)

PPSX
Data Types & Variables in JAVA
PDF
Java I/o streams
PPTX
Java Data Types
PPTX
Packages in java
PDF
input/ output in java
PPTX
Data types in java
PPTX
Type casting in java
PPTX
Java Strings
PPTX
Classes objects in java
PPTX
Java string handling
PPTX
Constructor and Types of Constructors
PPT
9. Input Output in java
PPTX
Data Types, Variables, and Operators
PPT
Java Streams
PPT
Method overriding
PDF
Method Overloading In Java
PPTX
Inter Thread Communicationn.pptx
PPTX
Pointers in c++
PPTX
Constructor in java
Data Types & Variables in JAVA
Java I/o streams
Java Data Types
Packages in java
input/ output in java
Data types in java
Type casting in java
Java Strings
Classes objects in java
Java string handling
Constructor and Types of Constructors
9. Input Output in java
Data Types, Variables, and Operators
Java Streams
Method overriding
Method Overloading In Java
Inter Thread Communicationn.pptx
Pointers in c++
Constructor in java
Ad

Similar to Constructor in java (20)

PDF
Constructors in Java (2).pdf
DOCX
Lecture11.docx
PPTX
constructors.pptx
PPTX
Java ConstructorsPPT.pptx
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
Constructor oopj
PPTX
CONSTRUCTORS.pptx
PPT
Constructors.16
PPTX
object oriented programming CONSTRUCTORS.pptx
PPTX
Classes, Objects and Method - Object Oriented Programming with Java
PPTX
Week 3-LectureA Object Oriented Programmings.pptx
PPTX
constructor & destructor in cpp
PPTX
constructor & destructor in cpp
PPTX
Constructor and desturctor
PPTX
object oriented programming using java, second sem BCA,UoM
PPTX
Constructors and destructors
PPTX
Constructors and Destructors
PDF
Constructors In Java – Unveiling Object Creation
PDF
1.11 Constructors and Destructors....pdf
Constructors in Java (2).pdf
Lecture11.docx
constructors.pptx
Java ConstructorsPPT.pptx
Lecture 4_Java Method-constructor_imp_keywords
A constructor in Java is a special method that is used to initialize objects
Constructor oopj
CONSTRUCTORS.pptx
Constructors.16
object oriented programming CONSTRUCTORS.pptx
Classes, Objects and Method - Object Oriented Programming with Java
Week 3-LectureA Object Oriented Programmings.pptx
constructor & destructor in cpp
constructor & destructor in cpp
Constructor and desturctor
object oriented programming using java, second sem BCA,UoM
Constructors and destructors
Constructors and Destructors
Constructors In Java – Unveiling Object Creation
1.11 Constructors and Destructors....pdf
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Trump Administration's workforce development strategy
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Trump Administration's workforce development strategy
Final Presentation General Medicine 03-08-2024.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming

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); } }