Monday, 8 February 2016

Method Overloading

By

  • A child class can modify the method which is inherited from parent class
  • Child class can create method with different functionality but with the same
    • Name
    • Return type
    • Arguments and order
  • This concept is known as Method Overloading
  • The overloaded method can't have weaker access specifier in the child class
class Customer{

public void display(){
  System.out.println("Method of Customer class");
}
}

class RegularCustomer extends Customer{
public void display(){
  System.out.println("Method of Regular customer class");
}

class RetailStore{
public static void main(String args[]){
 
      RegularCustomer regObj=new RegularCustomer();
 
      regObj.display();
}
}

Output: Method of Regular customer class

Why method of customer class is invoked?
because to invoke base class method we have to use super keyword
so if we make changes in Retail class 
class RetailStore{
public static void main(String args[]){

      RegularCustomer regObj=new RegularCustomer();
      super.display(); 
      regObj.display();
}
}
Output: Method of Customer class
             Method of Regular customer class

Note: When super is used to invoke the overridden method of parent class, it need not be the first statement in the child class method unlike the use of this()/ super() for constructor invocation

0 comments :

Post a Comment