Wednesday 8 August 2012

What is polymorphism? what is method overloading and overriding?



Polymorphism is a greek word meaning "many forms". The concept of polymorphism can be expressed by the phrase "one interface, multiple methods". Polymorphism is the concept that supports the capability of an object of a class to behave differently in response to a message or action.

For example. If you give 2+3, it results into 5 (the sum of 2 and 3). And if you give 'A'+'BC', it results into 'ABC', the contacenated strings. Hence the same operator ' + ' is doing two different operations (operator overloading) . This is polymorphism.
Hence polymorphism is a property by which the same message can be sent to objects of several  different classes, and each object can respond in a different way depending on its class.

Method overloading and method overriding are examples of polymorphism.



Function overloading :

When several function declarations are specified for a single function name in the same scope, the function is said to be overloaded. Functions with same name but different number or types of arguments are called overloaded functions.
For example:

float divide( int a, int b);
float divide( float x, float y);

Here divide() taking two int arguments is different from divide() taking two float arguments.
This is known as function overloading.

Another example:
void calc( int i);
void calc( char c);
void calc( float f);
void calc( double d);

these are overloaded functions and each of them can be treated as separate function with different declarations.

However,

1. if the signature of 2nd function is same as of first then the 2nd is treated as redeclaration of 1st.
    for ex.   void sqr( int a, float b);        //function 1
                 void sqr( int x, float y);        //same signature as that of function 1


2. if the signature of the two functions match but the return types differ, the compiler will give an error.
    For ex.
                  float square( float f);
                  double square( float x);    //error
 
   You can have different return types only if signatures are also different:
    float square( float f);             //diff signatures, hence
    double square ( double d);   //allowed

*Method overloading is compile time polymorphism



Method overriding :

When a method in a subclass has the same name and type signature as a method in its superclass, then method in subclass is said to override the method in superclass. 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.

For example:

class A
   {      int i,j;
           void show( )
           {   System.out.println ("show in base class");
           }
   }

class B extends A
   {     int k;
          void show( )
          {    System.out.println("show in sub class");
          }
   }

class override
   {
          public static void main( String args[] )
          {
                  B subobj = new B( );
                  subobj.show( );
          }
   }


When show( ) is invoked on an object of type B, the version of show( ) define within B is used, i.e the version of show( ) inside B overrides the version declared in A.
hence here output will be:

show in sub class


*Overridden methods allow java to support run time polymorphism



However if we want to access the superclass version of an overridden function we can do this by using keyword super.

if in show( ) of sub class we use:
:
:
void show( )      //in class B
{
      super.show( );              //calling the function show of its super class A
      System.out.println("show in sub class");
}
:
:
:
this will call the function of superclass and hence output will be:
show in super class
show in sub class



To know more about keyword super go to the article
use of keyword super in java


If you found the article helpful please like my page................

7 comments:

  1. Hi, I like that our extensive useful post. I would quickly understand our article blogs. I periodically read our blog post all content is really useful.Superb article. I read really experienced article here.
    Java Training Institute in Chennai
    Java Training in Velachery

    ReplyDelete