Friday, 21 October 2016

Polymorphism in java || Overloading and Overriding

Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

     In java there are two types of polymorphism: Runtime Polymorphism and compile time Polymorphism. Compile time Polymorphism can achieved by overloading static methods and Runtime Polymorphism can achieved by method overloading and method overriding. Here you can see both of these.

    Before to know how to achieve runtime Polymorphism and compile time Polymorphism, you need to know what is overloading and what is overriding. In java overloading and overriding is done with both methods and constructors.

Overloading: Overloading is a OOP's concept where a method or constructor takes more than one form by changing its signature in terms of its parameters type and number of parameters it have.

Overriding: Overriding is a situation where a sub class and super class have same method. But in sub class the method will perform different tasks. OR the process of writing a super class method in sub class with same signature to achieve different task is called overriding.

Compile time Polymorphism:

                            Compile time Polymorphism can achieved by overloading static methods. In java it is possible to overload static methods. Consider below example.

public class Test
{
public static void display()
{
System.out.println(" display with no parameters");
}
public static void display( int )
{
System.out.println(" display with integer parameters");
}
public static void main( String args)
{
       Test.display();
        Test.display(10);          // static methods are called suing class name only.

The above programs gives out put as

display with no parameters
display with integer parameters

Can we override a static method in java ?

 The answer is simple "NO", Because, when you define same static method in both sub class and super class, the sub class version of static method hides super class version of method.

Runtime Polymorphism

                 Run time Polymorphism can achieve by overloading methods. Method overloading is the process where you can define different with same name but different in their parameters. Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

Example:

 public class Test2
{
public int add( int a , int b)
{

 return(a+b);
}

public int add( int a, int b, int c)
{

      return( a+b+c)
}
public static void main( String args[])
{

    Test2 t= new Test2();
    System.out.println(t.add(2,3));
    System.out.println(t.add(2,3,4));
}

}

More details on Overloading and Overriding are available in up coming sessions.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top