0
24kviews
short note on Polymorphism.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 5 M

Year: Dec 2014

1 Answer
2
234views

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations.

  • It is a feature that allows one interface to be used for a general class of actions.
  • An operation may exhibit different behavior in different instances.
  • The behavior depends on the types of data used in the operation.
  • It plays an important role in allowing objects having different internal structures to share the same external interface.
  • Polymorphism is extensively used in implementing inheritance.

Following concepts demonstrate different types of polymorphism in java.

  1. Method Overloading
  2. Method Overriding

1.Method Overloading

In Java, it is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This concept is known as Method Overloading.

i. To call an overloaded method in Java, it is must to use the type and/or number of arguments to determine which version of the overloaded method to actually call.

ii. Overloaded methods may have different return types; the return type alone is insufficient to distinguish two versions of a method.

iii. 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.

iv. It allows the user to achieve compile time polymorphism.

v. An overloaded method can throw different exceptions.

vi. It can have different access modifiers.

Example:

class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}

Here the method demo() is overloaded 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used.

2. Method Overriding

Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding.

Example:

public class BaseClass
{   
    public void methodToOverride() //Base class method
    {
         System.out.println ("I'm the method of BaseClass");
    }
}
public class DerivedClass extends BaseClass
{
    public void methodToOverride() //Derived Class method
    {
         System.out.println ("I'm the method of DerivedClass");
    }
}

public class TestMethod
{
     public static void main (String args []) {
        // BaseClass reference and object
        BaseClass obj1 = new BaseClass(); 
        // BaseClass reference but DerivedClass object
        BaseClass obj2 = new DerivedClass(); 
        // Calls the method from BaseClass class
        obj1.methodToOverride(); 
        //Calls the method from DerivedClass class
        obj2.methodToOverride(); 
     }
}
Please log in to add an answer.