SlideShare a Scribd company logo
CONSTRUCTOR
S.G.V.P.Gunasekara - 2015238
1
Constructor
Constructor in java is a special type of method that is used to initialize the
object.
Java constructor is invoked at the time of object creation. It constructsthe
values i.e. provides data for the object that is why it is known as
constructor.
2
Rules for creating java constructor
There are basically two rules defined for the constructor.
1. Constructor name must be same as its classname
2. Constructor must have no explicit returntype
3
Types of java constructors
There are two types of constructors:
1. Default constructor (no-arg constructor)
2. Parameterizedconstructor
4
Default constructor
class. It will be invoked at the
A constructor that have no parameter is known as defaultconstructor.
Syntax of default constructor:
<class_name>(){}
Example of default constructor
In this example, we are creating the no-arg constructor in theBike
time of object creation.
class Bike1{
Bike1(){
System.out.println(&quot;Bike is created&quot;)
;}
public static void main(Stringargs[]){
Bike1 b=new Bike1();
}
}
5
Parameterized constructor
A constructor that have parameters is known as parameterizedconstructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to
the distinct objects.
6
 Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters.
We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+&quot; &quot;+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,&quot;Karan&quot;);
Student4 s2 = new Student4(222,&quot;Aryan&quot;);
s1.display();
s2.display();
}
}
7
Output:
111 Karan
222 Aryan
8
Constructor overloading
Like other methods in java constructor can be overloaded i.e. we can
create asmany constructors in our class asdesired. Number of
constructors depends on the information about attributes of an objectwe
have while creating objects.
9
example: constructor overloading example:
classLanguage{
String name;
Language() {
System.out.println("Constructor methodcalled.");
}
Language(String t) {
name = t;
}
public static void main(String[] args) {
Language cpp = new Language();
Language java = new Language("Java");
cpp.setName("C++");
java.getName();
cpp.getName();
}
void setName(String t) {
name = t;
}
void getName() {
System.out.println("Language name: " + name);
}
}
10
Output:
Constructor method called.
Language name: java
Language name:c++
11
12

More Related Content

What's hot (20)

PDF
ITFT-Classes and object in java
Atul Sehdev
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Abu Saleh
 
PDF
Dagger1
Ramesh Akula
 
DOCX
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
PPT
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
PPTX
Constructor destructor.ppt
Karthik Sekar
 
PPTX
An introduction to constructor
Niby Babu
 
PDF
Pavel kravchenko obj c runtime
DneprCiklumEvents
 
PPTX
Class object method constructors in java
Raja Sekhar
 
PPTX
Functions and Objects in JavaScript
Dhananjay Kumar
 
PDF
JavaScript Objects
Hazem Hagrass
 
PPTX
C++ classes
Aayush Patel
 
PPT
Mca 2nd sem u-2 classes & objects
Rai University
 
PPTX
Classes and objects1
Vineeta Garg
 
PPTX
19 reflection
dhrubo kayal
 
PPTX
Ian 2014.10.24 weekly report
LearningTech
 
PPTX
Constructor&method
Jani Harsh
 
PPTX
Classes and objects
rajveer_Pannu
 
ITFT-Classes and object in java
Atul Sehdev
 
Constructors and destructors
Vineeta Garg
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Abu Saleh
 
Dagger1
Ramesh Akula
 
Class notes(week 3) on class objects and methods
Kuntal Bhowmick
 
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructor destructor.ppt
Karthik Sekar
 
An introduction to constructor
Niby Babu
 
Pavel kravchenko obj c runtime
DneprCiklumEvents
 
Class object method constructors in java
Raja Sekhar
 
Functions and Objects in JavaScript
Dhananjay Kumar
 
JavaScript Objects
Hazem Hagrass
 
C++ classes
Aayush Patel
 
Mca 2nd sem u-2 classes & objects
Rai University
 
Classes and objects1
Vineeta Garg
 
19 reflection
dhrubo kayal
 
Ian 2014.10.24 weekly report
LearningTech
 
Constructor&method
Jani Harsh
 
Classes and objects
rajveer_Pannu
 

Similar to Constructor oopj (20)

PPTX
Constructor in java
Madishetty Prathibha
 
PDF
Constructors in Java (2).pdf
kumari36
 
PPTX
constructors.pptx
Epsiba1
 
PPTX
Constructors in java
chauhankapil
 
PDF
Constructors In Java – Unveiling Object Creation
Geekster
 
PPTX
Week 3-LectureA Object Oriented Programmings.pptx
FarhanGhafoor7
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PDF
Chapter 7 - Constructors.pdf
KavitaHegde4
 
PPTX
Chapter 7 - Constructors.pptx
KavitaHegde4
 
PPTX
Constructor in java
SIVASHANKARIRAJAN
 
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
PPTX
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
PPTX
Java Constructor
MujtabaNawaz4
 
PPTX
A constructor in Java is a special method that is used to initialize objects
Kavitha S
 
PPT
packages and interfaces
madhavi patil
 
PPTX
Constructors in java
sunilchute1
 
PPTX
UNIT - IIInew.pptx
akila m
 
PDF
Constructors and destructors
Prof. Dr. K. Adisesha
 
PPTX
1_JavIntro
SARJERAO Sarju
 
Constructor in java
Madishetty Prathibha
 
Constructors in Java (2).pdf
kumari36
 
constructors.pptx
Epsiba1
 
Constructors in java
chauhankapil
 
Constructors In Java – Unveiling Object Creation
Geekster
 
Week 3-LectureA Object Oriented Programmings.pptx
FarhanGhafoor7
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Chapter 7 - Constructors.pdf
KavitaHegde4
 
Chapter 7 - Constructors.pptx
KavitaHegde4
 
Constructor in java
SIVASHANKARIRAJAN
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
Java Constructor
MujtabaNawaz4
 
A constructor in Java is a special method that is used to initialize objects
Kavitha S
 
packages and interfaces
madhavi patil
 
Constructors in java
sunilchute1
 
UNIT - IIInew.pptx
akila m
 
Constructors and destructors
Prof. Dr. K. Adisesha
 
1_JavIntro
SARJERAO Sarju
 
Ad

More from ShaishavShah8 (19)

PPTX
Diffie hellman key algorithm
ShaishavShah8
 
PPTX
Clipping computer graphics
ShaishavShah8
 
PPTX
Classification of debuggers sp
ShaishavShah8
 
PPTX
Parallel and perspective projection in 3 d cg
ShaishavShah8
 
PPTX
Asymptotic notations ada
ShaishavShah8
 
PPT
Arrays in java oopj
ShaishavShah8
 
PPTX
Classical cyphers python programming
ShaishavShah8
 
PPTX
Logics for non monotonic reasoning-ai
ShaishavShah8
 
PPTX
Rdd transformations bda
ShaishavShah8
 
PPT
Introduction to data warehouse dmbi
ShaishavShah8
 
PPT
Lan, wan, man mcwc
ShaishavShah8
 
PPT
Introduction to xml, uses of xml wt
ShaishavShah8
 
PPTX
Agile process se
ShaishavShah8
 
PPTX
Applications of huffman coding dcdr
ShaishavShah8
 
PPTX
Cookie management using jsp a java
ShaishavShah8
 
PPTX
Login control .net
ShaishavShah8
 
PPTX
Rdd transformations
ShaishavShah8
 
PPTX
LAN, WAN, MAN
ShaishavShah8
 
PPTX
Introduction to data warehouse
ShaishavShah8
 
Diffie hellman key algorithm
ShaishavShah8
 
Clipping computer graphics
ShaishavShah8
 
Classification of debuggers sp
ShaishavShah8
 
Parallel and perspective projection in 3 d cg
ShaishavShah8
 
Asymptotic notations ada
ShaishavShah8
 
Arrays in java oopj
ShaishavShah8
 
Classical cyphers python programming
ShaishavShah8
 
Logics for non monotonic reasoning-ai
ShaishavShah8
 
Rdd transformations bda
ShaishavShah8
 
Introduction to data warehouse dmbi
ShaishavShah8
 
Lan, wan, man mcwc
ShaishavShah8
 
Introduction to xml, uses of xml wt
ShaishavShah8
 
Agile process se
ShaishavShah8
 
Applications of huffman coding dcdr
ShaishavShah8
 
Cookie management using jsp a java
ShaishavShah8
 
Login control .net
ShaishavShah8
 
Rdd transformations
ShaishavShah8
 
LAN, WAN, MAN
ShaishavShah8
 
Introduction to data warehouse
ShaishavShah8
 
Ad

Recently uploaded (20)

PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
The Future of Artificial Intelligence (AI)
Mukul
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 

Constructor oopj

  • 2. Constructor Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructsthe values i.e. provides data for the object that is why it is known as constructor. 2
  • 3. Rules for creating java constructor There are basically two rules defined for the constructor. 1. Constructor name must be same as its classname 2. Constructor must have no explicit returntype 3
  • 4. Types of java constructors There are two types of constructors: 1. Default constructor (no-arg constructor) 2. Parameterizedconstructor 4
  • 5. Default constructor class. It will be invoked at the A constructor that have no parameter is known as defaultconstructor. Syntax of default constructor: <class_name>(){} Example of default constructor In this example, we are creating the no-arg constructor in theBike time of object creation. class Bike1{ Bike1(){ System.out.println(&quot;Bike is created&quot;) ;} public static void main(Stringargs[]){ Bike1 b=new Bike1(); } } 5
  • 6. Parameterized constructor A constructor that have parameters is known as parameterizedconstructor. Why use parameterized constructor? Parameterized constructor is used to provide different values to the distinct objects. 6
  • 7.  Example of parameterized constructor In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor. class Student4{ int id; String name; Student4(int i,String n){ id = i; name = n; } void display(){System.out.println(id+&quot; &quot;+name);} public static void main(String args[]){ Student4 s1 = new Student4(111,&quot;Karan&quot;); Student4 s2 = new Student4(222,&quot;Aryan&quot;); s1.display(); s2.display(); } } 7
  • 9. Constructor overloading Like other methods in java constructor can be overloaded i.e. we can create asmany constructors in our class asdesired. Number of constructors depends on the information about attributes of an objectwe have while creating objects. 9
  • 10. example: constructor overloading example: classLanguage{ String name; Language() { System.out.println("Constructor methodcalled."); } Language(String t) { name = t; } public static void main(String[] args) { Language cpp = new Language(); Language java = new Language("Java"); cpp.setName("C++"); java.getName(); cpp.getName(); } void setName(String t) { name = t; } void getName() { System.out.println("Language name: " + name); } } 10
  • 11. Output: Constructor method called. Language name: java Language name:c++ 11
  • 12. 12