Chapter 4:
Polymorphism
Assuming different forms
                           1
               Introduction
• Polymorphism literally means “being able to
  assume different forms.”
• Polymorphism is an important and powerful
  concept in OOP.
• It is the ability to process objects in a hierarchy
  differently depending on their actual class type.
• B/c of polymorphism, you can easily create
  extensible classes.
• In OOP, calling a method is often referred to as
  sending a message to an object.
• The object responds to the message by
  executing the appropriate method.
                                                    2
            Polymorphism
• Polymorphism just means that different objects
  can respond to the same message in different
  ways.
• Polymorphism can work for both variables/states
  and methods/behaviours of objects.
• However, two powerful polymorphic concepts
  are often useful when you define a class:
  method overloading and method overriding.
   Java implements polymorphism through method
    overloading and method overriding.
                                                  3
      Method Overloading
• In the java language, a method is uniquely
  identified by its name and a list of parameters.
• Therefore, it is legal to define two methods with
  the exact same name, but with different
  parameters.
• In such cases, the methods are said to be
  overloaded, and the process is referred to as
  method overloading.
• Method overloading allows you to reuse the
  same method name repeatedly, but with varying
  parameters and results.
• Method overloading is actually not limited to use
  with inheritance.                                4
        Method overloading…
• You can overload methods in the same class
• You can overload methods defined in a
  superclass.
• You have been calling an overloaded method
  since the beginning of this course.
   The println() method you call via the
    System.out variable is an overloaded method.
   Ten versions of this method are defined, each with
    a different parameter type.
   Every version of println() method prints the
    parameter that you pass to it and adds a carriage
    return to the end.
                                                    5
        Method overloading…
   The alternative to println() approach is to define a
    method for each possible type.
     • A printString(),
     • printInt(), and
     • printChar() method can be defined.
   Do you see the benefit of method overloading
    now?
   Without it, you would have to memorize all those
    methods to print data.
• When an overloaded method is invoked, Java
  uses the type and/or number of arguments as its
  guide to determine which version of the
  overloaded method to actually call.
                                                       6
         Method overloading…
• Thus, overloaded methods must differ in the
  type and/or number of their parameters.
• While overloaded methods may have different
  return types, the return type alone is insufficient
  to distinguish two versions of a method.
• When Java encounters a call to an overloaded
  method, it simply executes the version of the
  method whose parameters match the
  arguments used in the call.
                                                    7
public class Overloaded{
     public void sayHello(){
          System.out.println("Hello");
     }
     public void sayHello(String name){
          System.out.println("Hello " + name);
     }
     public void sayHello(String name, int count){
          for (int i=0; i<count; i++)
               sayHello(name);
     }
     public static void main(String args[]){
          Overloaded overloaded = new Overloaded();
          overloaded.sayHello();
          overloaded.sayHello("Abraham");
          overloaded.sayHello("Gudeta", 3);
       }
}
                                                      8
        Method overloading…
• Here are the important facts concerning
  overloaded methods.
   When you overload a method, the name must be
    exactly the same, and the parameter list must be
    different. There is no escaping this rule.
   Although you can have a different return type, it is
    not enough to only have a different return type.
   Keep in mind that you can overload methods
    defined in the same class or any superclass.
   A final method can be overloaded.
   A static method can be overloaded.
                                                      9
        Method overriding
• When a method in a subclass has the same
  name and type signature as a method in its
  superclass, then the method in the subclass is
  said to override the method in the superclass.
• A method overrides an existing method iff it has
  the same return type and exactly equal number
  of parameters.
• When an overridden method is called from
  within a subclass, it will always refer to the
  version of that method defined by the subclass.
• The version of the method defined by the
  superclass will be hidden.
                                                 10
                  Method overriding…
class A {                                  class B extends A {
  int i, j;                                  int k;
  A(int a, int b) {                          B(int a, int b, int c) {
      i = a;                                   super(a, b);
        j = b;                                 k = c;
    }                                          }
    //display i and j                         void show() {
    void show() {                                  System.out.println("k: " + k);
System.out.println("i and j: " + i + " "       }
  + j);
       }                                   }
}
                                                                                11
           Method overriding…
   If you wish to access the superclass version of an
    overridden function, you can do so by using
    super keyword in the same way we have
    discussed in inheritance.
• Little important points about method overriding:
   Methods with differing type signatures are
    overloaded – not overridden.
   Methods cannot be overridden in the same class.
   final method cannot be overridden.
                                                     12
class B extends A {
  int k;
  B(int a, int b, int c) {
          super(a, b);
          k = c;
  }
    void show() {
           super.show(); // this calls A's show()
           System.out.println("k: " + k);
    }
}
                                                    13