KEMBAR78
Constructor overloading & method overloading | PPTX
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

Constructor overloading & method overloading

  • 1.
  • 2.
    CONTENT 1. constructors 2. typesof constructors 3. constructor overloading
  • 3.
    CONSTRUCTOR • Constructor injava 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 CREATINGJAVA 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 JAVACONSTRUCTORS • There are two types of constructors: 1. Default constructor (no-arguments constructor) 2. Parameterized constructor
  • 6.
    DEFAULT CONSTRUCTOR • Aconstructor that have no parameter is known as default constructor. • Syntax of default constructor: <class_name>() { }
  • 7.
    EXAMPLE class Bike{ Bike(){ System.out.println("Bike iscreated"); } public static void main(String args[]){ Bike b=new Bike(); } }
  • 8.
    PARAMETERIZED CONSTRUCTOR • Aconstructor 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; Stringname; 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 INJAVA • 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
  • 12.