---
title: "Polymorphism in Java"  
description: "In this article I am explaining about Polymorphism in Java"  
author: "Manoj Pandey"  
published: 2015-04-18  
updated: 2017-12-05  
canonical: https://www.mindstick.com/articles/1729/polymorphism-in-java  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Polymorphism in Java

[Polymorphism](https://www.mindstick.com/articles/32/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](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) 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](https://www.mindstick.com/articles/12033/method-overloading-in-c-sharp) (Compile time Polymorphism)

2. [Method overriding](https://www.mindstick.com/articles/12227/method-overloadind-and-method-overriding-in-c-sharp) (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](https://www.mindstick.com/forum/33583/selector-creation-with-method-name-and-parameter) but different – different parameter. This concept is [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) Method Overloading.

##### For example

```
   public void setData()     {                            }
     public void setData(String text)
     {


     }      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](https://answers.mindstick.com/qa/35072/how-to-check-if-two-string-are-anagram) 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](https://www.mindstick.com/blog/398/difference-between-object-type-and-var-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](https://www.mindstick.com/forum/33638/protected-access-modifier-c-sharp)

\
· [Abstract methods](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression) must be overridden

\
· Static and final methods cannot be overridden

\
· Constructors cannot be overridden

\
· It is also known as Runtime polymorphism.

---

Original Source: https://www.mindstick.com/articles/1729/polymorphism-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
