Object Oriented Programming in Dart: Polymorphism

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?

Dhruv Nakum
Β·Oct 10, 2022Β·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • ⏳️ In The Previous Article...
  • What is Polymorphism πŸ€·πŸ»β€β™‚οΈ?
  • Types of Polymorphism
  • A Big no To Method Overloading in Dart❌
  • Advantages of Polymorphism
  • Conclusion

⏳️ 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.

HereWeGoAgainGtaGIF.gif

  • 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:
The class can do the same things in different ways.

GiveMeOneExampleShowMeGIF.gif

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.

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

  6. 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().
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....

PeaceOutImOutGIF.gif

Did you find this article valuable?

Support Dhruv Nakum by becoming a sponsor. Any amount is appreciated!

See recent sponsors |Β Learn more about Hashnode Sponsors
Β 
Share this