What is an example of a polymorphism?
Polymorphism is a key concept in Object-Oriented Programming (OOP) that enhances flexibility and integration within your code. By allowing objects of different classes to be treated as objects of a common superclass, polymorphism enables methods to perform different functions based on the object they are acting upon. Understanding polymorphism is crucial for designing scalable and maintainable software systems, and it's a common topic in technical interviews.
Example of Polymorphism
Consider a simple application that manages different types of employees in a company. You have a base class Employee
and several subclasses like Manager
, Developer
, and Intern
. Each subclass has its own implementation of the calculateSalary()
method.
Real-Life Example
Imagine a company where employees have different roles, and each role has a unique way of calculating salaries:
- Manager: Receives a base salary plus a bonus.
- Developer: Receives a base salary plus stock options.
- Intern: Receives an hourly wage without additional benefits.
By using polymorphism, you can treat all these employees uniformly while allowing each type to calculate its salary differently.
// Base class public abstract class Employee { protected String name; public Employee(String name) { this.name = name; } public abstract double calculateSalary(); } // Subclass for Managers public class Manager extends Employee { private double baseSalary; private double bonus; public Manager(String name, double baseSalary, double bonus) { super(name); this.baseSalary = baseSalary; this.bonus = bonus; } @Override public double calculateSalary() { return baseSalary + bonus; } } // Subclass for Developers public class Developer extends Employee { private double baseSalary; private double stockOptions; public Developer(String name, double baseSalary, double stockOptions) { super(name); this.baseSalary = baseSalary; this.stockOptions = stockOptions; } @Override public double calculateSalary() { return baseSalary + stockOptions; } } // Subclass for Interns public class Intern extends Employee { private double hourlyWage; private int hoursWorked; public Intern(String name, double hourlyWage, int hoursWorked) { super(name); this.hourlyWage = hourlyWage; this.hoursWorked = hoursWorked; } @Override public double calculateSalary() { return hourlyWage * hoursWorked; } } // Using polymorphism public class Company { public static void main(String[] args) { Employee[] employees = { new Manager("Alice", 80000, 15000), new Developer("Bob", 70000, 5000), new Intern("Charlie", 20, 100) }; for (Employee emp : employees) { System.out.println(emp.name + "'s Salary: $" + emp.calculateSalary()); } } }
Output:
Alice's Salary: $95000.0
Bob's Salary: $75000.0
Charlie's Salary: $2000.0
In this example:
- The
Employee
class defines an abstract methodcalculateSalary()
. - Each subclass (
Manager
,Developer
,Intern
) provides its own implementation ofcalculateSalary()
. - The
Company
class demonstrates polymorphism by treating all employees as instances ofEmployee
and calling thecalculateSalary()
method without needing to know the specific subclass type.
Benefits of Polymorphism
- Flexibility: Allows you to add new classes with different behaviors without modifying existing code.
- Maintainability: Simplifies code maintenance by reducing dependencies between classes.
- Reusability: Promotes code reuse by enabling common interfaces for different implementations.
- Scalability: Makes it easier to scale applications by adding new functionalities with minimal changes.
Recommended Courses
To further enhance your understanding of polymorphism and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the Coding Interview: Patterns for Coding Questions
- Grokking the System Design Interview
These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.
GET YOUR FREE
Coding Questions Catalog