SlideShare a Scribd company logo
1 of 17
CLASS WITH
CONSTRUCTORS
Prepared by: Assi. Professor Sejal Jadav
• Q- class with Constructor.
• Constructors are used to initialize the object’s
state.
• Like methods, a constructor also contains
collection of statements(i.e. instructions) that are
executed at time of Object creation.
Rules of Constructor:
• The constructor is a special method which has the same name
as its class.
• It initializes the object of its class automatically at the time of
creation.
• One important point about constructor is that they have
no return type. Not even void.
• This is because the implicit return type of a class’ constructor is
the class type itself.
• When we create an object, we call the constructor after new
keyword.
• So the statements written in the constructor are executed at
the time.
• Constructors can use any access modifier, including private.
• The constructor name must match the name of the class.
• Constructors must not have a return type.
• It's legal (but stupid) to have a method with the same name
as the class.
Class classname
{
Type instance variable1;
Type instance variable2;
Classname() //Non parameter constructor
{
Body of constructor1;
}
Classname(parameter list) // Parameterize constructor.
{
Body of constructor2;
}
Type methodname1(parameter list)
{
Body of methods;
}
}
Types of constructor
There are two type of constructor in Java:
No-argument constructor:
• A constructor that has no parameter is known as default
constructor.
• If we don’t define a constructor in a class, then compiler
creates default constructor(with no arguments) for the
class.
• And if we write a constructor with arguments or no-argument
then compiler does not create default constructor.
•
Default constructor provides the default values to the object
like 0, null etc. depending on the type.
class Abc
{
int num;
String name;
// this would be invoked while object
// of that class created.
Abc()
{
System.out.println("Constructor called");
}
}
class Para_No
{
public static void main (String[] args)
{
// this would invoke default constructor.
Abc a1 = new Abc();
// Default constructor provides the default
// values to the object like 0, null
System.out.println(a1.name);
System.out.println(a1.num);
}
}
2)Parameterized Constructor:
• A constructor that has parameters is known as
parameterized constructor.
• If we want to initialize fields of the class(static variable)
with your own values, then use parameterized constructor.
// parameterized constructor.
class XYZ
{
// data members of the class.
String name;
int id;
/** constructor would initialized data members
with the values of passed arguments while
object of that class created. */
XYZ(String name, int id)
{
this.name = name;
this.id = id;
} }
class With_Para
{
public static void main (String[] args)
{
// this would invoke parameterized constructor.
XYZ x1 = new XYZ(“raj", 1);
System.out.println("Name :" + x1.name + " and Id :" + x1.id);
}
}
Does constructor return any value?
• There are no “return value” statements in constructor.
Constructor Overloading
• Like methods, we can overload constructors for creating
objects in different ways.
• Compiler differentiates constructors on the basis of
numbers of parameters, types of the parameters and order
of the parameters.
• For example:
class Demo
{
// constructor with one argument
Demo(String name)
{
System.out.println("Constructor with one " +
"argument - String : " + name);
}
// constructor with two arguments
Demo(String name, int age)
{
System.out.print("Constructor with two arguments : " +
" String and Integer : " + name + " "+ age);
}
// Constructor with one argument but with different
// type than previous..
Demo(long id)
{
System.out.println("Constructor with one argument : " +
"Long : " + id);
}
}
class OverLoading_Demo
{
public static void main(String[] args)
{
// type 'String'.
Demo d2 = new Demo("Ram");
// Invoke the constructor with two arguments
Demo d3 = new Demo("Dharmesh", 26);
// Invoke the constructor with one argument of type 'Long'.
Demo d4 = new Demo(325614567);
}
}
Constructor

More Related Content

What's hot

constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Interface result set
Interface result setInterface result set
Interface result setmyrajendra
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructorsuraj pandey
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSaharsh Anand
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 

What's hot (20)

constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Interface result set
Interface result setInterface result set
Interface result set
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Constructor
ConstructorConstructor
Constructor
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Lesson3
Lesson3Lesson3
Lesson3
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 

Similar to Constructor

Similar to Constructor (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Oops
OopsOops
Oops
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
Constructor
ConstructorConstructor
Constructor
 
BCA Class and Object.pptx
BCA Class and Object.pptxBCA Class and Object.pptx
BCA Class and Object.pptx
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Java ConstructorsPPT.pptx
Java ConstructorsPPT.pptxJava ConstructorsPPT.pptx
Java ConstructorsPPT.pptx
 
Java Constructor
Java ConstructorJava Constructor
Java Constructor
 
Constructors in JAva.pptx
Constructors in JAva.pptxConstructors in JAva.pptx
Constructors in JAva.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Definning class.pptx unit 3
Definning class.pptx unit 3Definning class.pptx unit 3
Definning class.pptx unit 3
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
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
 
C++ constructors
C++ constructorsC++ constructors
C++ constructors
 
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
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Core java day4
Core java day4Core java day4
Core java day4
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event HandlingJadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingJadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout ManagersJadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15Jadavsejal
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14Jadavsejal
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13Jadavsejal
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12Jadavsejal
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10Jadavsejal
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
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
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.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
 

Constructor

  • 1. CLASS WITH CONSTRUCTORS Prepared by: Assi. Professor Sejal Jadav
  • 2. • Q- class with Constructor. • Constructors are used to initialize the object’s state. • Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.
  • 3. Rules of Constructor: • The constructor is a special method which has the same name as its class. • It initializes the object of its class automatically at the time of creation. • One important point about constructor is that they have no return type. Not even void. • This is because the implicit return type of a class’ constructor is the class type itself. • When we create an object, we call the constructor after new keyword.
  • 4. • So the statements written in the constructor are executed at the time. • Constructors can use any access modifier, including private. • The constructor name must match the name of the class. • Constructors must not have a return type. • It's legal (but stupid) to have a method with the same name as the class.
  • 5. Class classname { Type instance variable1; Type instance variable2; Classname() //Non parameter constructor { Body of constructor1; } Classname(parameter list) // Parameterize constructor. { Body of constructor2; } Type methodname1(parameter list) { Body of methods; } }
  • 6. Types of constructor There are two type of constructor in Java: No-argument constructor: • A constructor that has no parameter is known as default constructor. • If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. • And if we write a constructor with arguments or no-argument then compiler does not create default constructor. • Default constructor provides the default values to the object like 0, null etc. depending on the type.
  • 7. class Abc { int num; String name; // this would be invoked while object // of that class created. Abc() { System.out.println("Constructor called"); } }
  • 8. class Para_No { public static void main (String[] args) { // this would invoke default constructor. Abc a1 = new Abc(); // Default constructor provides the default // values to the object like 0, null System.out.println(a1.name); System.out.println(a1.num); } }
  • 9. 2)Parameterized Constructor: • A constructor that has parameters is known as parameterized constructor. • If we want to initialize fields of the class(static variable) with your own values, then use parameterized constructor.
  • 10. // parameterized constructor. class XYZ { // data members of the class. String name; int id; /** constructor would initialized data members with the values of passed arguments while object of that class created. */ XYZ(String name, int id) { this.name = name; this.id = id; } }
  • 11. class With_Para { public static void main (String[] args) { // this would invoke parameterized constructor. XYZ x1 = new XYZ(“raj", 1); System.out.println("Name :" + x1.name + " and Id :" + x1.id); } }
  • 12. Does constructor return any value? • There are no “return value” statements in constructor.
  • 13. Constructor Overloading • Like methods, we can overload constructors for creating objects in different ways. • Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters. • For example:
  • 14. class Demo { // constructor with one argument Demo(String name) { System.out.println("Constructor with one " + "argument - String : " + name); } // constructor with two arguments Demo(String name, int age) { System.out.print("Constructor with two arguments : " + " String and Integer : " + name + " "+ age); }
  • 15. // Constructor with one argument but with different // type than previous.. Demo(long id) { System.out.println("Constructor with one argument : " + "Long : " + id); } }
  • 16. class OverLoading_Demo { public static void main(String[] args) { // type 'String'. Demo d2 = new Demo("Ram"); // Invoke the constructor with two arguments Demo d3 = new Demo("Dharmesh", 26); // Invoke the constructor with one argument of type 'Long'. Demo d4 = new Demo(325614567); } }