SlideShare a Scribd company logo
Submitted to :-
MR. R.S.Rawat Sir
(H.O.D. of it department)
Submitted by:-
Puneet tiwari
B.e. viith sem
enroll no.:-0601it131034
 contents
• What is Constructor.
• Properties of constructor.
• Difference B/w constructors and methods.
• Types of constructors.
• Constructor overloading.
• This();--constructor call
 What is Constructor
• The constructor is a special method used to
initializing object.
• A block of code which has to be executed on
creation of object is defined inside constructor.
• It is used to initialize non static variables of a class.
Properties of constructor
• Constructor name is same name as class name.
• Constructor does not have return value not even
void.
• Constructors only allows access modifiers.
• It is called implicitly at time of creating object.
• It is called only once on one object.
 Difference between constructors
and methods
• Method is called any number of times on the object.
• Constructor is called only once on object.
• Method is called explicitly.
• Constructor is called implicitly.
• Methods allow access and non access modifiers.
• It Allows only access modifiers.
• Method name can be any name.
• Constructor name must be same as class name.
• Methods are inherited.
• These are not inherited.
 Types of constructors
Constructors
User defined
constructors
Non parameterized
constructors
Parameterized
constructors
Compiler defined
constructors
Default
1. Compiler defined constructor
• If there is no constructors within a class compiler
provide default constructor without any
parameter this is called as default constructor.
• Default constructor access modifier is same as
class.
 Example
Abc.java Abc.class
Class Abc
{
int x;
int y;
}
Class Abc
{
int x;
int y;
Abc()
{
super();
}
}
2. User defined constructor
• Constructor defined by programmer is called the user
defined constructor.
I. Non parameterized constructor– A constructor
written in a class without any parameter is called as
non parameterized constructor.
This constructor does not receive any value.
 Example
class A
{
A()
{
System.out.println(“Inside Constructor ”);
}
void A()
{
System.out.println(“Inside Method”);
}
}
Class Main
{
public static void main(String [ ] args)
{
A obj1=new A();
obj1.A();
}
}
Output –
Inside Constructor
Inside Method
II. Parameterized constructor- parameterized constructor is
user defined constructor.
• This constructor receive values at the time of creating objects
• This constructor allow two types of parameters.
i. Primitive types.
ii. Reference types.
• If constructor having primitive type it receive value.
• If constructor having reference type it receive address of object.
 Example
class cons
{
private int no;
private String name;
cons(int no,String name)
{
this.no=no;
this.name=name;
}
void display()
{
System.out.println(no);
System.out.println(name);
}
}
class Main
{
public static void main(String [ ] args)
{
cons obj1=new cons(101,"Puneet");
obj1.display();
}
output-
101
puneet
Constructor overloading
• Defining more than one constructor within a class with same
name is called as constructor overloading.
• The constructors must be change in at least one criteria.
I. Number of parameters.
II. Types of parameters.
III. Order of parameters.
• Constructor is overloaded to extend functionality of existing
constructor.
 Example
class Abc
{
Abc()
{
System.out.println("Non parameterized constructor");
}
Abc(int a,String s)
{
System.out.println(a);
System.out.println(s);
}
}
class Main
{
public static void main(String [ ] args)
{
Abc obj1=new Abc();
Abc obj2=new Abc(601,"Igec Sagar");
}
output-
Non parameterized constructor
601
Igec Sagar
Constructor call-using this();
• One constructor call another constructor of same
class using “this()” constructor call.
• It allows code reusability.
• It is used inside the constructor but not inside the
method.
 Example
class Abc
{
Abc()
{
System.out.println("Non parameterized constructor");
}
Abc(int x)
{
this();
System.out.println("parameterized constructor");
}
}
class Main
{
public static void main(String [ ] args)
{
Abc obj1=new Abc(10);
}
}
Output--
Non parameterized constructor
Parameterized constructor
Java is like a sea if you are too much thirsty then only you can able to learn it as whole--.
Constructors

More Related Content

What's hot

[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
Learn By Watch
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
kunal kishore
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Constructor
ConstructorConstructor
Constructor
Bastin Rogers
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
Samad Qazi
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
Prem Kumar Badri
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
Pallavi Seth
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
Nusrat Gulbarga
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
aleenaguen
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
Da Mystic Sadi
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
Constructors
ConstructorsConstructors
Constructors
M Vishnuvardhan Reddy
 
Constructor& destructor
Constructor& destructorConstructor& destructor
Constructor& destructor
chauhankapil
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 

What's hot (20)

[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor
ConstructorConstructor
Constructor
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructor
ConstructorConstructor
Constructor
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
What is Constructors and Destructors in C++ (Explained with Example along wi...
What is Constructors and Destructors in  C++ (Explained with Example along wi...What is Constructors and Destructors in  C++ (Explained with Example along wi...
What is Constructors and Destructors in C++ (Explained with Example along wi...
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
Constructors
ConstructorsConstructors
Constructors
 
Constructor& destructor
Constructor& destructorConstructor& destructor
Constructor& destructor
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 

Similar to Constructors

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
sasukeman
 
25csharp
25csharp25csharp
25csharp
Sireesh K
 
25c
25c25c
Oops
OopsOops
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143
sanjeet kumar
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
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
Lovely Professional University
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
Asif Shahzad
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
BCA Class and Object.pptx
BCA Class and Object.pptxBCA Class and Object.pptx
BCA Class and Object.pptx
SarthakSrivastava70
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
nivedita murugan
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
KavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
KavitaHegde4
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Madishetty Prathibha
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 

Similar to Constructors (20)

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
25csharp
25csharp25csharp
25csharp
 
25c
25c25c
25c
 
Oops
OopsOops
Oops
 
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
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
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
BCA Class and Object.pptx
BCA Class and Object.pptxBCA Class and Object.pptx
BCA Class and Object.pptx
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 

Recently uploaded

ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 

Constructors

  • 1. Submitted to :- MR. R.S.Rawat Sir (H.O.D. of it department) Submitted by:- Puneet tiwari B.e. viith sem enroll no.:-0601it131034
  • 2.  contents • What is Constructor. • Properties of constructor. • Difference B/w constructors and methods. • Types of constructors. • Constructor overloading. • This();--constructor call
  • 3.  What is Constructor • The constructor is a special method used to initializing object. • A block of code which has to be executed on creation of object is defined inside constructor. • It is used to initialize non static variables of a class.
  • 4. Properties of constructor • Constructor name is same name as class name. • Constructor does not have return value not even void. • Constructors only allows access modifiers. • It is called implicitly at time of creating object. • It is called only once on one object.
  • 5.  Difference between constructors and methods • Method is called any number of times on the object. • Constructor is called only once on object. • Method is called explicitly. • Constructor is called implicitly. • Methods allow access and non access modifiers. • It Allows only access modifiers. • Method name can be any name. • Constructor name must be same as class name. • Methods are inherited. • These are not inherited.
  • 6.  Types of constructors Constructors User defined constructors Non parameterized constructors Parameterized constructors Compiler defined constructors Default
  • 7. 1. Compiler defined constructor • If there is no constructors within a class compiler provide default constructor without any parameter this is called as default constructor. • Default constructor access modifier is same as class.
  • 8.  Example Abc.java Abc.class Class Abc { int x; int y; } Class Abc { int x; int y; Abc() { super(); } }
  • 9. 2. User defined constructor • Constructor defined by programmer is called the user defined constructor. I. Non parameterized constructor– A constructor written in a class without any parameter is called as non parameterized constructor. This constructor does not receive any value.
  • 10.  Example class A { A() { System.out.println(“Inside Constructor ”); } void A() { System.out.println(“Inside Method”); } } Class Main { public static void main(String [ ] args) { A obj1=new A(); obj1.A(); } } Output – Inside Constructor Inside Method
  • 11. II. Parameterized constructor- parameterized constructor is user defined constructor. • This constructor receive values at the time of creating objects • This constructor allow two types of parameters. i. Primitive types. ii. Reference types. • If constructor having primitive type it receive value. • If constructor having reference type it receive address of object.
  • 12.  Example class cons { private int no; private String name; cons(int no,String name) { this.no=no; this.name=name; } void display() { System.out.println(no); System.out.println(name); } } class Main { public static void main(String [ ] args) { cons obj1=new cons(101,"Puneet"); obj1.display(); } output- 101 puneet
  • 13. Constructor overloading • Defining more than one constructor within a class with same name is called as constructor overloading. • The constructors must be change in at least one criteria. I. Number of parameters. II. Types of parameters. III. Order of parameters. • Constructor is overloaded to extend functionality of existing constructor.
  • 14.  Example class Abc { Abc() { System.out.println("Non parameterized constructor"); } Abc(int a,String s) { System.out.println(a); System.out.println(s); } } class Main { public static void main(String [ ] args) { Abc obj1=new Abc(); Abc obj2=new Abc(601,"Igec Sagar"); } output- Non parameterized constructor 601 Igec Sagar
  • 15. Constructor call-using this(); • One constructor call another constructor of same class using “this()” constructor call. • It allows code reusability. • It is used inside the constructor but not inside the method.
  • 16.  Example class Abc { Abc() { System.out.println("Non parameterized constructor"); } Abc(int x) { this(); System.out.println("parameterized constructor"); } } class Main { public static void main(String [ ] args) { Abc obj1=new Abc(10); } } Output-- Non parameterized constructor Parameterized constructor
  • 17. Java is like a sea if you are too much thirsty then only you can able to learn it as whole--.