SlideShare a Scribd company logo
1 of 8
Constructors
 Constructor is a special method of a class which will invoke 
automatically whenever instance or object of class is created. 
 Constructor name should be same as class name. If we want to 
create constructor in a class we need to create a constructor 
method name same as class name. 
ex: 
<AccessModifier/static/Nothing> <ClassName>() 
{ 
//Initialization 
}
 class can have any number of constructors and constructors 
don’t have any return type, not even void 
 Constructors are responsible for object initialization and memory 
allocation of its class. 
 There is always at least one constructor in every class.
Types of Constructors 
1. Default Constructor 
2. Parameterless Constructor 
3. Parameterized Constructor 
4. Copy Constructor 
5. Private Constructor 
6. Static Constructor
Default Constructor: 
Default constructor will be added by compiler, if we do not 
write any constructor in a class. Default constructor does not have any 
parameters . 
Parameterless Constructor: 
If we do not write any parameters in a constructor is called as 
parameterless constructor. 
Parameterized Constructor: 
A constructor with at least one parameter is called as 
parameterized constructor.
Copy Constructor: 
A parameterized constructor that contains a parameter of 
same class type is called as copy constructor. 
Main purpose of copy constructor is to initialize new instance 
to the values of an existing instance. 
Ex: 
<AccessModifier/Nothing> <ClassName>(ClassName cn) 
{ 
}
Private Constructor: 
A private constructor is a special instance constructor. 
The main purpose of creating private constructor is used to 
restrict the class from being instantiated. 
Ex: 
<private/Nothing> <ClassName>() 
{ 
}
Static Constructor: 
 When we declared constructor as static it will be invoked only 
once for any number of instances of the class. 
 Static constructor is used to initialize static fields of the class and 
to write the code that needs to be executed only once. 
 Static constructor will not accept any parameters because it is 
automatically called by CLR. 
 Static constructor will not have any access modifiers. 
 Static constructor will execute automatically whenever we create 
first instance of class. 
 Only one static constructor will allowed.

More Related Content

What's hot

JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive OverviewMohamed Loey
 
Prototype Pattern
Prototype PatternPrototype Pattern
Prototype PatternIder Zheng
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScriptOffirmo
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructorsAsif Shahzad
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersLaurent Duveau
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript FundamentalsSunny Sharma
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder paramisoft
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviWinston Levi
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painSander Mak (@Sander_Mak)
 

What's hot (20)

JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive Overview
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 
Csharp
CsharpCsharp
Csharp
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Prototype Pattern
Prototype PatternPrototype Pattern
Prototype Pattern
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
TypeScript
TypeScriptTypeScript
TypeScript
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
 
Introduction to Angular for .NET Developers
Introduction to Angular for .NET DevelopersIntroduction to Angular for .NET Developers
Introduction to Angular for .NET Developers
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Singleton Pattern
Singleton PatternSingleton Pattern
Singleton Pattern
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 

Similar to Constructors (20)

ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
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
 
Oops
OopsOops
Oops
 
22c
22c22c
22c
 
22csharp
22csharp22csharp
22csharp
 
Constructor
ConstructorConstructor
Constructor
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
DHARANI.pptx
DHARANI.pptxDHARANI.pptx
DHARANI.pptx
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
C++
C++C++
C++
 
C++
C++C++
C++
 

More from Yaswanth Babu Gummadivelli (20)

Presentation on BA
Presentation on BAPresentation on BA
Presentation on BA
 
ERP
ERPERP
ERP
 
Ba -content
Ba -contentBa -content
Ba -content
 
E commerce use case documentation.
E commerce use case documentation.E commerce use case documentation.
E commerce use case documentation.
 
MOM on activity diagram
MOM on activity diagramMOM on activity diagram
MOM on activity diagram
 
UML Diagrams
UML DiagramsUML Diagrams
UML Diagrams
 
Business analyst ppt
Business analyst pptBusiness analyst ppt
Business analyst ppt
 
MOM on BA
MOM on BAMOM on BA
MOM on BA
 
exception handling
 exception handling exception handling
exception handling
 
collections
 collections collections
collections
 
array
array array
array
 
Use case for atm
Use case for atmUse case for atm
Use case for atm
 
use case diagramHospital managment system
use case diagramHospital managment systemuse case diagramHospital managment system
use case diagramHospital managment system
 
Activity diagram for ticket vending machine
Activity diagram for ticket vending machineActivity diagram for ticket vending machine
Activity diagram for ticket vending machine
 
Extreme programming
Extreme programmingExtreme programming
Extreme programming
 
Agile model
Agile model Agile model
Agile model
 
Business Analyst
Business AnalystBusiness Analyst
Business Analyst
 
CRM and ERP
CRM and ERPCRM and ERP
CRM and ERP
 
Reflection
ReflectionReflection
Reflection
 
Bill Gates
Bill GatesBill Gates
Bill Gates
 

Recently uploaded

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Constructors

  • 2.  Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created.  Constructor name should be same as class name. If we want to create constructor in a class we need to create a constructor method name same as class name. ex: <AccessModifier/static/Nothing> <ClassName>() { //Initialization }
  • 3.  class can have any number of constructors and constructors don’t have any return type, not even void  Constructors are responsible for object initialization and memory allocation of its class.  There is always at least one constructor in every class.
  • 4. Types of Constructors 1. Default Constructor 2. Parameterless Constructor 3. Parameterized Constructor 4. Copy Constructor 5. Private Constructor 6. Static Constructor
  • 5. Default Constructor: Default constructor will be added by compiler, if we do not write any constructor in a class. Default constructor does not have any parameters . Parameterless Constructor: If we do not write any parameters in a constructor is called as parameterless constructor. Parameterized Constructor: A constructor with at least one parameter is called as parameterized constructor.
  • 6. Copy Constructor: A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance. Ex: <AccessModifier/Nothing> <ClassName>(ClassName cn) { }
  • 7. Private Constructor: A private constructor is a special instance constructor. The main purpose of creating private constructor is used to restrict the class from being instantiated. Ex: <private/Nothing> <ClassName>() { }
  • 8. Static Constructor:  When we declared constructor as static it will be invoked only once for any number of instances of the class.  Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.  Static constructor will not accept any parameters because it is automatically called by CLR.  Static constructor will not have any access modifiers.  Static constructor will execute automatically whenever we create first instance of class.  Only one static constructor will allowed.