0

Every answer on stack overflow provides information on how to replace switch or if else with polymorphism but the switch, if else constructs also help in providing different behavior of an object with respect to context and inputs.

Why if else,switch is not considered as a part of polymorphism.

deshmanth
  • 529
  • 5
  • 12
  • 4
    I suggest that you read more about polymorphism. It is related to class inheritance which has nothing to do with if statements. – Code-Apprentice Aug 30 '18 at 04:02
  • Can you please exemplify? – everton Aug 30 '18 at 04:08
  • @everton What is "exeplify"? – Code-Apprentice Aug 30 '18 at 04:09
  • Possible duplicate of [How can Polymorphism replace an if-else statement inside of a loop?](https://stackoverflow.com/questions/519515/how-can-polymorphism-replace-an-if-else-statement-inside-of-a-loop) – everton Aug 30 '18 at 04:14
  • @everton I mentioned the same in the question that every answer refers to how to replace but not why isn't it considered to be a part of it – deshmanth Aug 30 '18 at 04:16
  • Consider what happens when you introduce a new type of object later. If you worked with if and switch you would have to change every place where the objects are used. – Henry Aug 30 '18 at 04:24
  • @Code-Apprentice isn't polymorphism also can be done without inheritance – deshmanth Aug 30 '18 at 04:25
  • @deshmanth There are many kinds of polymorphism. The type most commonly used in Java uses inheritance. – Code-Apprentice Aug 30 '18 at 04:27
  • @Henry yes using switch for providing different behaviour is a bad practice as it violates Open-Closed principle. But my question is why isn't switch or if else considered as a part of polymorphism – deshmanth Aug 30 '18 at 04:28
  • 2
    "why isn't switch or if else considered as a part of polymorphism ": by definition. – Henry Aug 30 '18 at 04:29
  • The topic of polymorphism takes at least a whole chapter in a textbook on object oriented programming. This is way too big to discuss in the format provided by this site. I suggest you read more tutorials or textbooks on object oriented programming and practice using it by writing code. This is the only way you will gain a deeper understanding. – Code-Apprentice Aug 30 '18 at 04:29

2 Answers2

3

Conditional becomes a code smell when we have to check an object’s type in order to make some logic or behavior decision. It doesn’t matter whether it is a stack of if/else block or a switch statement.This violates open-closed principle.

The open closed principle states that the entities(classes, modules, functions etc) should be open for extension,but closed for modification.Which means that these entities won't be allowed to make changes in its source code.

This can be achieved through Abstraction and Polymorphism.

Benifits of Polymorphism over Conditionals

  1. instead of asking an object about its state and then performing actions based on this, it is much easier to simply tell the object what it needs to do and let it decide for itself how to do that.
  2. Removes duplicate code. You get rid of many almost identical conditionals.
  3. If you need to add a new execution variant, all you need to do is add a new subclass without touching the existing code (Open/Closed Principle).
Roman
  • 4,922
  • 3
  • 22
  • 31
Kethiri Sundar
  • 480
  • 2
  • 12
  • yes sundar I agree with you but my question is not how to replace the switch with polymorphism but why isn't switch considered a part of polymorphism – deshmanth Aug 30 '18 at 04:18
  • 2
    @deshmanth - It's because of the first sentence in his answer. Code that needs to test for the type of an object is not polymorphic. If you introduce a new type that conforms to the contract (e.g., a new class that implements a `Shape` interface), your `switch` or `if/else` statement would no longer work correctly until you modified it to test for that new type. That's the opposite of polymorphism, which requires that client code only needs to know that the object conforms to the required interface (a.k.a. contract). – Ted Hopp Aug 30 '18 at 04:35
  • thanks @TedHopp.this gave a good insight for my question. – deshmanth Aug 30 '18 at 04:45
1

Polymorphism in a concept that allows an Object to retain behavior and properties from its parent classes. Explained further here and here

if, else and switch are procedural code constructs.

The two have nothing in common.

Edit: Thanks @Ted Hopp, correcting my post.

pvik
  • 105
  • 5
  • 1
    To OP's point, I think he/she refers to code, which does switching logic, that it's recommended, as a good OOP practice, to be refactored under polimorphism. – everton Aug 30 '18 at 04:08
  • Polymorphism has to do with conformance to a contract, not with object-oriented programming. OOP uses inheritance to implement polymorphism, but in many languages you can find polymorphism implemented without inheritance. OOP and polymorphism are related, but distinct concepts. – Ted Hopp Aug 30 '18 at 04:27
  • @TedHopp Since the question is tagged [tag:java], it is easy, and most likely correct, to assume that the OP is learning about inheritance-based polymorphism. – Code-Apprentice Aug 30 '18 at 04:31
  • nope.polymorphism using method overloading – deshmanth Aug 30 '18 at 04:33