Object Oriented Programming in Dart: Polymorphism
What is Polymorphism? What are Method Overloading and Method Overriding? What are the real-world examples and Advantages of 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:
Poly( Many
)morph(Form
)ism means an object with Many Form.
- 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:
Example
You can walk. A Dog can walk. Bird can walk.
A single remote is used to control all kinds of TVs.
In all kinds of electronic items, Switching on and off performs the same operation.
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.
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.)
If a class has multiple methods having the same name but different parameters, It is known as method overloading. Method Overriding (Run-Time Poly.)
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding
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()
.
- Consider a Bank class, which has a method named
class Bank {
getROI(){
return 0;
}
}
- Now consider 3 banks that extend this Bank class. All these 3 banks have their different Rate of Interests.
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 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....
Β