# Object Oriented Programming in Dart: Polymorphism

## ⏳️ In The Previous Article...
- Hey there 👋🏻, Welcome to the fifth and final article in the series on **OOPs in Dart**. Encapsulation was covered in the prior article.
- We saw a practical example of what encapsulation is. Then, we learned how to accomplish encapsulation in Dart.
- We will learn about polymorphism in this article. Therefore, let's begin.

-------

## What is Polymorphism 🤷🏻‍♂️?
- If you've heard the word "polymorphism," you may be familiar with this definition:
<center>*Poly(`Many`)morph(`Form`)ism means an object with **Many Form**.* </center>

![HereWeGoAgainGtaGIF.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1665221280956/AlrQpBOde.gif align="center")

- Well, the explanation given above does fit the term's meaning. But what in the world does that mean for programming, exactly?
- So, in programming, it means:

<center>The class can do the same things in different ways.</center>


![GiveMeOneExampleShowMeGIF.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1665222671360/063q7wTz9.gif align="center")


### Example

1. You can **walk**. A Dog can **walk**. Bird can **walk**.

2. A single remote is used to control all kinds of TVs.

3. In all kinds of electronic items, *Switching on and off* performs the same operation.

4. In programming, the operator `+` performs addition with numbers But performs concatenation when used with Strings. 

- As you can see, **Walking**, **Remote**, **Switching On-Off**, and Operator **+** behave differently in different situations. This in a nutshell is called Polymorphism 

- Let's look at how we can achieve polymorphism via code.

```dart
class Abc {
  void add(int x, int y) {
    print("num addition=" + (a + b));
  }

  void add(double x, double y) {
    print("floating num addition=" + (a + b));
  }

  String add(String x, String y) {
    return (x + y);
  }
}
```

- As you can see, here the method **add()** behaves differently in different conditions.
- It's adding two numbers when used with Int and Float, and performs Concatenation when used with Strings 

----------
## Types of Polymorphism 

- In programming languages like JAVA, there are two types of Polymorphism.

 - **Method Overloading (Compile Time Poly.)**
  - <center> If a class has multiple methods having the same name but different parameters, It is known as method overloading.</center>

 - **Method Overriding (Run-Time Poly.)**
  - <center>If subclass (child class) has the same method as declared in the parent class, it is known as method overriding</center>


- The example we saw above of **add()** is nothing but an example of *Method Overloading*.

- Let's also take one example of Method Overriding.
 - Consider a Bank class, which has a method named `getROI()`.

```dart
class Bank {
  getROI(){
    return 0;
  }
}
```

 - Now consider 3 banks that extend this Bank class. All these 3 banks have their different Rate of Interests. 

```dart
class Bank1 extends Bank {
  @override
  getROI(){
    return 2;
  }
}

class Bank2 extends Bank {
  @override
  getROI(){
    return 5;
  }
}

class Bank3 extends Bank {
  @override
  getROI(){
    return 9;
  }
}
```

- As you can see, all these Banks are overriding the Parent Bank class's `getROI` method. This is method overriding.

-------

## A Big no To Method Overloading in Dart❌ 
- In Dart, Function overloading is not supported. Function overloading requires static types. Dart at its core is a dynamically typed language.
- But you can use **Optional Parameters**
- You can read the discussion [here](https://github.com/dart-lang/language/issues/1741) on why dart doesn't support method overloading.


--------

## Advantages of Polymorphism
- One benefit of polymorphism is that it enables code reuse, which may make things simpler to understand and manage.
- Through the use of a common interface, polymorphism enables us to communicate with a variety of distinct classes while hiding implementation details.

-------

## Conclusion
-  The value of polymorphism is more obvious in statically-typed languages than dynamic ones. 
- But since almost all dynamic languages support at least one kind of polymorphism, the value is clearly universal.
- We saw how polymorphism allows us to formally define types in a way that leaves the part of the definition open to extension.
- It also allows us to write clear, clean, simply-structured code.
- Additionally, it achieves the core goal or motto of OOPs, which is to connect programming with the real world.

--------

- **I hope you enjoyed and learned something from this article. If you have any feedback/queries, leave them in the comments.**
- **Thank you for spending time reading this article. See you in the next article. Until then....**


![PeaceOutImOutGIF.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1665226469869/xDxo8gWkW.gif align="left")

- Connect with me on [Twitter](https://twitter.com/dhruv_nakum), [LinkedIn](https://www.linkedin.com/in/dhruv-nakum-4b1054176/), and [Github](https://github.com/red-star25).

