Replace switch-case with Polymorphism in Object-Oriented Programming

By: fyvo August 4, 2025 Python

Description

Switch-case statements can be replaced with polymorphism to improve scalability and maintainability.

Original Code (Outdated)

switch(type) {
  case "circle": drawCircle(); break;
  case "square": drawSquare(); break;
}

Updated Code (Modern)

interface Shape { void draw(); }
class Circle implements Shape { public void draw() { /*...*/ } }
class Square implements Shape { public void draw() { /*...*/ } }
Shape shape = getShape(type);
shape.draw();

Discussion (0)