Sunday, 17 August 2014

Method Overloading and Overriding



Method Overloading :-  It is a part of static polymorphism. Overloading means writing multiple methods with same name but with different parameters. In case of overloading return type of a method is not concerned, but sequence of parameter, number of parameter and type of parameter is concerned.

Following code snippet displays the demo for method overloading.


package coreCollection;

public class Overloading extends SuperClass {

//Method with no parameter.
public void show(){
System.out.println("call for show with out prms");
}

       //medhod for int parameter and look at the return type.
        public int show(int a){
System.out.println("Call for show with Int prm : "+a);
return a;
}
    
    //method with different type and number of parameter.
    public void show(long a, int b){
    System.out.println("Call for different type and number of parameter. : int : "+a+", long : "+b);
}
    
    //method with different sequence of parameter.
    public void show(int a,long b){
    System.out.println("Call for different sequence of parameter : int : "+a+", long : "+b);
    }
    
    // Overloading from SuperClass.
    public void display(String str){
System.out.println("Overloading from SuperClass : "+str);
}

    
public static void main(String[] args) {

Overloading oao=new Overloading();
//calling without parameter.
oao.show();

//calling of int type of parameter. int a=oao.show(2); System.out.println("value return from method with int type parameter : "+a);

//calling with int and long type parameter.
oao.show(2, 12l);

//calling with long and int type parameter.
oao.show(12l,2);

// calling SuperClass overloading method.
SuperClass sc=new Overloading();
((Overloading)sc).display("stringParam");

}

}

//Super Class
class SuperClass{

public void display(){
System.out.println("Super Class medhod.");
}
}

Output:










Method Overriding :- It is a part of dynamic polymorphism. Overriding means sub-class should contain method with the same name and parameters. Return type of method should be same or sub-class of the super-class's method's return type. Accessibility of overriding method should be higher than overriden method.


package coreCollection;

class SuperClass1{
//method having void return type and default access specifier.
void show(){
System.out.println("Super Class show method.");
}

//method having return type as number.
Number display(){
System.out.println("Super Class display method.");
return 1;
}
}

//Sub-class
public class Overriding extends SuperClass1{

 //access specifier is higher than super method.
   public void show(){
System.out.println("show method of sub-class.");  
   }
  
  // overriding method having return type as subtype of supermethod's return type.
  protected Integer display(){
System.out.println("display method of sub-class.");
return 2;
  } 


   public static void main(String[] args) {
    
  //calling methods of sub-class.
  Overriding ovr=new Overriding();
  ovr.display();
  ovr.show();
  
  //calling method of super-class.
  SuperClass1 sc=new SuperClass1();
  sc.display();
  sc.show();
  
  //calling method of sub-class with the reference of super-class.
  SuperClass1 ssc=new Overriding();
  ssc.display();
  ssc.show();

   }
}


Output : 


No comments:

Post a Comment