JaiswalTraining
Get the online training

Polymorphism

  • Polymorphism is a feature that allows one interface to be used for a general class
    of actions that is one interface with multiple methods.
  • One name Many Form
  • Generally, polymorphism allows you to mix methods and objects of
    different types in a consistent way.
Method Overloading

  • The process of declaring methods with the same name but different parameter types.
  • A method can be overloaded in the same class or in a subclass.
  • Which overloaded method to call is based on reference type and decided at compile time.
  • Which overridden method to call is based on object type and decided during runtime.


Rules:
  • Overloaded methods must change the argument list.
  • Overloaded methods can change the return type.
  • Overloaded methods can change the access modifier.
  • Overloaded methods can declare new or broader checked exceptions.


public static float max(float a, float b)
public static float max(float a, float b, float c)
public static int max(int a, int b)
When a method is called, the call signature is matched to the correct method version.

Method Overriding

  • Allows a subclass to redefine methods of the same name from the superclass.
  • The key benefit of overriding is the ability to define/defer behavior specific to subclasses.
  • Which overridden method to call is based on object type and decided at runtime.

Rules:-

  • An overridden method must have
    • the same name
    • the same number of parameters and types
    • the same return type
      as the overrided method.
  • Overriding a method cannot narrow the method access level defined in the overridden method.
  • Overriding a method cannot widen checked exceptions defined in the overridden method.
  • Methods declared as  private, static, or final cannot be overridden
Dynamic (or late) binding:
  • The code executed for a method call is associated with the call during run-time.
  • The actual method executed is determined by the type of the object
    and not the type of the reference.
  • Allows superclass and subclass objects to be accessed in a regular and consistent way:
  • Array or collection of superclass references can be used to access
    a mixture of superclass and subclass objects
  • This is very useful if you want access collections of mixed data types
    (for example draw different graphical objects using the same draw() method call for each)

Example:-1

public class addition {

  public int  sum(int a,int b)  {
    return (a+b);
  }
  
  private void sum(float  a,float b)  {
    System.out.println(a+b);
  }
  
   protected String  sum(String  a,String b)  {
    System.out.println(a+b);
    return(a+b);
  }
  public static void main(String[] args) {
    addition a=new addition();
    a.sum(11.4f,33.5f);
    a.sum("ram""kumar");
    a.sum(1112);

  }

}
Outpt


ava addition
44.9
ramkumar
Download code

Example:-2



public class overload1 {

  void disp(String name)throws InterruptedException 
  {}
  void disp(Float n)throws Exception
  {}
  void disp(Double n)throws ArithmeticException
    {}
  void disp(Number n)throws RuntimeException
  {}
  
  public static void main(String[] args) {
    overload1 o=new overload1();
    o.disp(11.11);
    o.disp(11.11f);
    o.disp("11.11");
  }
}
Output:-

javac overload1.java
overload1.java:16: unreported exception java.lang.Exception; must be caught or d
eclared to be thrown
                o.disp(11.11f);
                      ^
overload1.java:17: unreported exception java.lang.InterruptedException; must be
caught or declared to be thrown
                o.disp("11.11");
                      ^
2 errors
Download Code

Example:=3

public class school {
  public school(String name) {
    System.out.println("school name is "+name);
 }
}

class highschool extends school{
  
  public highschool(int pin) {
    super("dkv");
   System.out.println("school pin code  is "+pin);
  }
  
  public highschool(String name) {
    
    this(110092);
  System.out.println("school name is "+name);
  }
  
  public static void main(String[] args) {
  
    highschool high=new highschool("dps");
  }
  
}
Output:-


java highschool
school name is dkv
school pin code  is 110092
school name is dps
Download code

Example:-4:

class animal {
  void sound() {
    System.out.println("animal voice ");
  }
}

public class dog extends animal {
  void sound() {
    System.out.println("dog voice ");
  }

  public static void main(String[] args) {
    dog d = new dog();
    animal a = new animal();

    //a = d;
    a.sound();
    //d = (dog) a;
    d.sound();    
  }
}
Output :-


java dog
animal voice
dog voicee
Download code

Example:5-

public class shape {
static  String draw(String s){
   System.out.println("draw the shape");
   return "hello";
 
}

class rectangle extends shape{
  
  String draw(String s) {
     System.out.println("draw the rectagle");
     return "hello";
  }
}

class square extends shape{
  
public   String  draw(String s) {
     System.out.println("draw the square");
     return "hello";
  }
}

class circle extends shape{
  
  String draw(String s) {
     System.out.println("draw the circle");
     return "hello";
  }
  public static void main(String[] args) {
    shape c=new circle();
    shape r=new rectangle();
    shape s=new square();
    
    c.draw("3");
    r.draw("44.77");
    s.draw("33.5f");  
    
  }
}
Output:-

C:\wamp\www\corejava\polymorphism\code>javac shape.java
shape.java:10: draw(java.lang.String) in rectangle cannot override draw(java.lan
g.String) in shape; overridden method is static
        String draw(String s) {
               ^
shape.java:18: draw(java.lang.String) in square cannot override draw(java.lang.S
tring) in shape; overridden method is static
public  String  draw(String s) {
                ^
shape.java:26: draw(java.lang.String) in circle cannot override draw(java.lang.S
tring) in shape; overridden method is static
        String draw(String s) {
               ^
3 errors


Afetr modifying the code  applying static keyword to draw method
public class shape {
static  String draw(String s){
   System.out.println("draw the shape");
   return "hello";
 
}

class rectangle extends shape{
  
  static String draw(String s) {
     System.out.println("draw the rectagle");
     return "hello";
  }
}

class square extends shape{
  
public   static String  draw(String s) {
     System.out.println("draw the square");
     return "hello";
  }
}

class circle extends shape{
  
  static String draw(String s) {
     System.out.println("draw the circle");
     return "hello";
  }
  public static void main(String[] args) {
    shape c=new circle();
    shape r=new rectangle();
    shape s=new square();
    
    c.draw("3");
    r.draw("44.77");
    s.draw("33.5f");  
    
  }
}

Output:-


java circle
draw the shape
draw the shape
draw the shape
Dowload code