Constructor overloading & method overloading

garishmabhatia 3,594 views 12 slides Feb 24, 2019
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Constructor overloading & method overloading IN CORE-JAVA


Slide Content

Constructor overloading By : Garishma bhatia

content constructors types of constructors 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. C onstructor 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
Tags