SlideShare a Scribd company logo
7
Most read
10
Most read
11
Most read
CONSTRUCTOR
OVERLOADING
BY : GARISHMA BHATIA
CONTENT
1. constructors
2. types of constructors
3. constructor overloading
CONSTRUCTOR
• Constructor in java is a special type of method that is used to initialize the object.
• Java constructor is invoked at the time when the object is created.
RULES FOR CREATING JAVA CONSTRUCTOR
• There are basically three rules defined for the constructor.
1. Constructor name must be same as its class name.
2. Constructor must have no explicit return type.
3. Constructor cannot be abstract, static, final, and synchronized.
TYPES OF JAVA CONSTRUCTORS
• There are two types of constructors:
1. Default constructor (no-arguments constructor)
2. Parameterized constructor
DEFAULT CONSTRUCTOR
• A constructor that have no parameter is known as default
constructor.
• Syntax of default constructor:
<class_name>()
{
}
EXAMPLE
class Bike{
Bike(){
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
PARAMETERIZED CONSTRUCTOR
• A constructor which has a specific number of parameters is called a
parameterized constructor.
• Syntax :
<Class name>(<datatype> <variable name>, <datatype> <variable name>)
{
//data members;
}
EXAMPLE
class Student{
int id;
String name;
Student(int i,String n) {
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
CONSTRUCTOR OVERLOADING IN JAVA
• Constructor overloading is a technique of having more than one constructor with
different parameter lists.
• They are arranged in a way that each constructor performs a different task.
• They are differentiated by the compiler by the number of parameters in the list
and their Signatures.
• class Student5{
• int id;
• String name;
• int age;
• Student5(int i,String n){
• id = i;
• name = n;
• }
• Student5(int i,String n,int a){
• id = i;
• name = n;
• age=a;
• }
• void display(){System.out.println(id+" "+name+" "+age);
• }
• public static void main(String args[]){
• Student5 s1 = new Student5(111,"Karan");
• Student5 s2 = new Student5(222,"Aryan",25);
• s1.display();
• s2.display();
• }
• }
EXAMPLE
Thank you

More Related Content

PPTX
Constructor in java
PPTX
Big Data Analytics
PPTX
Access specifiers(modifiers) in java
PPS
Java Exception handling
PPTX
Static keyword ppt
PPTX
Analysis of variance (anova)
PDF
Collections in Java Notes
PDF
Method Overloading In Java
Constructor in java
Big Data Analytics
Access specifiers(modifiers) in java
Java Exception handling
Static keyword ppt
Analysis of variance (anova)
Collections in Java Notes
Method Overloading In Java

What's hot (20)

PPTX
Method overloading
PPTX
Operators in java
PPTX
Java package
PPTX
Constructors in java
PPTX
Interface in java
PPTX
OOPS In JAVA.pptx
PPTX
Constructor and Types of Constructors
PPTX
Java Data Types
PPTX
JAVA AWT
PPTX
Ppt on this and super keyword
PPS
Introduction to class in java
PPTX
Constructor in java
PPTX
constructors in java ppt
PPTX
Type casting in java
PPT
Method overriding
PPTX
Control statements in java
PPTX
I/O Streams
PPTX
Abstract class in c++
PPTX
Classes objects in java
PPT
Introduction to method overloading &amp; method overriding in java hdm
Method overloading
Operators in java
Java package
Constructors in java
Interface in java
OOPS In JAVA.pptx
Constructor and Types of Constructors
Java Data Types
JAVA AWT
Ppt on this and super keyword
Introduction to class in java
Constructor in java
constructors in java ppt
Type casting in java
Method overriding
Control statements in java
I/O Streams
Abstract class in c++
Classes objects in java
Introduction to method overloading &amp; method overriding in java hdm
Ad

Similar to Constructor overloading & method overloading (20)

PPTX
Constructor oopj
PPTX
Constructors in java
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
constructors.pptx
PPTX
BCA Class and Object.pptx
PPTX
Constructor in java Presentation by Dawood Khan .pptx
PDF
Chapter 7 - Constructors.pdf
PPTX
Chapter 7 - Constructors.pptx
PPTX
Java Constructor
PPTX
Constructors in JAva.pptx
PPTX
Java Constructors
PPTX
Constructors
PDF
Constructors in Java (2).pdf
PPTX
Jaga codinghshshshshehwuwiwijsjssnndnsjd
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPT
Constructor
PPT
PPTX
UNIT - IIInew.pptx
PPT
Introduction to Java(basic understanding).ppt
Constructor oopj
Constructors in java
A constructor in Java is a special method that is used to initialize objects
UNIT 3- Java- Inheritance, Multithreading.pptx
constructors.pptx
BCA Class and Object.pptx
Constructor in java Presentation by Dawood Khan .pptx
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pptx
Java Constructor
Constructors in JAva.pptx
Java Constructors
Constructors
Constructors in Java (2).pdf
Jaga codinghshshshshehwuwiwijsjssnndnsjd
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Constructor
UNIT - IIInew.pptx
Introduction to Java(basic understanding).ppt
Ad

More from garishma bhatia (19)

PPTX
Peephole optimization techniques
PPTX
Vm migration techniques
PPTX
Denial of service
PPTX
Iot + cloud
PPTX
WORKFLOW OF THE PROCESS IN SPM
PPTX
MULTILEVEL QUEUE SCHEDULING
PPT
MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS
PPTX
BLOCKCHAIN TECHNOLOGY
PPTX
Knapsack problem
PPTX
PRIM'S ALGORITHM
PPTX
Data types IN JAVA
PPTX
Data Algorithms And Analysis
DOCX
VEHICLE REGISTRATION PROJECT
PPTX
WIFI TECH
PPTX
MATCHING GRAPH THEORY
PPTX
Garishma se
PPTX
Garishma xcs
PPTX
Garishma maTHS
PPTX
Garishma ec
Peephole optimization techniques
Vm migration techniques
Denial of service
Iot + cloud
WORKFLOW OF THE PROCESS IN SPM
MULTILEVEL QUEUE SCHEDULING
MULTIPLE ACCESS PROTOCOL COMPUTER NETWORKS
BLOCKCHAIN TECHNOLOGY
Knapsack problem
PRIM'S ALGORITHM
Data types IN JAVA
Data Algorithms And Analysis
VEHICLE REGISTRATION PROJECT
WIFI TECH
MATCHING GRAPH THEORY
Garishma se
Garishma xcs
Garishma maTHS
Garishma ec

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Digital Logic Computer Design lecture notes
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Well-logging-methods_new................
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Digital Logic Computer Design lecture notes
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
UNIT 4 Total Quality Management .pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mechanical Engineering MATERIALS Selection
Internet of Things (IOT) - A guide to understanding
Sustainable Sites - Green Building Construction
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Automation-in-Manufacturing-Chapter-Introduction.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
Well-logging-methods_new................

Constructor overloading & method overloading

  • 2. CONTENT 1. constructors 2. types of constructors 3. constructor overloading
  • 3. CONSTRUCTOR • Constructor in java is a special type of method that is used to initialize the object. • Java constructor is invoked at the time when the object is created.
  • 4. RULES FOR CREATING JAVA CONSTRUCTOR • There are basically three rules defined for the constructor. 1. Constructor name must be same as its class name. 2. Constructor must have no explicit return type. 3. Constructor cannot be abstract, static, final, and synchronized.
  • 5. TYPES OF JAVA CONSTRUCTORS • There are two types of constructors: 1. Default constructor (no-arguments constructor) 2. Parameterized constructor
  • 6. DEFAULT CONSTRUCTOR • A constructor that have no parameter is known as default constructor. • Syntax of default constructor: <class_name>() { }
  • 7. EXAMPLE class Bike{ Bike(){ System.out.println("Bike is created"); } public static void main(String args[]){ Bike b=new Bike(); } }
  • 8. PARAMETERIZED CONSTRUCTOR • A constructor which has a specific number of parameters is called a parameterized constructor. • Syntax : <Class name>(<datatype> <variable name>, <datatype> <variable name>) { //data members; }
  • 9. EXAMPLE class Student{ int id; String name; Student(int i,String n) { id = i; name = n; } void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); s1.display(); s2.display(); } }
  • 10. CONSTRUCTOR OVERLOADING IN JAVA • Constructor overloading is a technique of having more than one constructor with different parameter lists. • They are arranged in a way that each constructor performs a different task. • They are differentiated by the compiler by the number of parameters in the list and their Signatures.
  • 11. • class Student5{ • int id; • String name; • int age; • Student5(int i,String n){ • id = i; • name = n; • } • Student5(int i,String n,int a){ • id = i; • name = n; • age=a; • } • void display(){System.out.println(id+" "+name+" "+age); • } • public static void main(String args[]){ • Student5 s1 = new Student5(111,"Karan"); • Student5 s2 = new Student5(222,"Aryan",25); • s1.display(); • s2.display(); • } • } EXAMPLE