Home > DeveloperSection > Articles > Polymorphism in Java

Polymorphism in Java


Java Java 
Ratings:
0 Comment(s)
 546  View(s)
Rate this:

Polymorphism in Java

Polymorphism is a OOPS concepts which can perform single action by different ways. Polymorphism is the concept to master if you want to master object-oriented programming because Java is an object-oriented language, it makes sense that you should learn the concepts and power of polymorphism in Java. Polymorphism is the capability of a method to do different things based on the object that it is acting upon.

There are two types of polymorphism in java

1.      Method overloading (Compile time Polymorphism)

2.      Method overriding (Runtime Polymorphism)

Method is a set of code which is referred to by name and can be called at any point in a program simply my utilizing the method’s name.

1.      Method overloading-: In java when we have same method name but different – different parameter. This concept is known as Method Overloading.

For example

   public void setData()

     {            

          

     }

     public void setData(String text)

     {

          

          

     }

Here is two same name method setData which have different- different argument first takes two integer argument and second takes two String argument.  I am creating a sample of method overloading.

 

public class Demo {

     public static void main(String args[]) {

           SampleOverload sample = new SampleOverload();

          System.out.println("Add Integer: " + sample.setData(5, 15));

System.out.println("Add String: "+sample.setData("Zack", " Anderson"));

     }

 

}

 

class SampleOverload {

 

     public String setData(int a, int b) {

           a = a + b;

           return String.valueOf(a);

 

     }

 

     public String setData(String text1, String text2) {

           text1 = text1 + text2;

           return text1;

 

     }

}

Output -:

Add Integer: 20

Add String: Zack Anderson

 

Rules for method overloading

·         Overloading can take place in the same or in its sub-class.

·         Constructor in Java can be overloaded

·         Overloaded methods must have a different argument list.

·         Overloaded method should always be in part of the same class, with same name but different parameters.

·         The parameters may differ in their type or number, or in both.

·         They may have the same or different return types.

·         It is also known as compile time polymorphism.

 

2.      Method Overriding-: It also known as run time method overloading. When we have same method name, same method parameter but in different class.

For example

 

class Sample

{

    public String setData(int a)

      {

        }

}

class Demo extends Sample

 {

   public String setData(int a)

      {

         }

}

Here is two methods setData but in different class .They have same parameter and same return type. Class must be extends each other. Here I am also creating a sample of Method overriding

 

public class Demo {

     public static void main(String args[]) {

           MyOverrideClass sample1 = new SampleOverload();

           MyOverrideClass sample2 = new MyOverrideClass();

     System.out.println("Add Integer: " + sample1.setData(5, 15));

     System.out.println("Multiplication Integer: " +

       sample2.setData(5, 15));

     }

 

}

 

class SampleOverload extends MyOverrideClass {

 

     public String setData(int a, int b) {

           a = a + b;

           return String.valueOf(a);

 

     }

 

}

 

class MyOverrideClass {

     public String setData(int a, int b) {

           a = a * b;

           return String.valueOf(a);

     }

}

Output -:

Add Integer: 20

Multiplication Integer: 75

 

Rules for method overriding

·         applies only to inherited methods

·         object type (NOT reference variable type) determines which overridden method will be used at runtime

·         Overriding methods must have the same return type

·         Overriding method must not have more restrictive access modifier

·         Abstract methods must be overridden

·         Static and final methods cannot be overridden

·         Constructors cannot be overridden

·         It is also known as Runtime polymorphism.

 


Don't want to miss updates? Please click the below button!

Follow MindStick