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 method calculateSalary().
  • Each subclass (Manager, Developer, Intern) provides its own implementation of calculateSalary().
  • The Company class demonstrates polymorphism by treating all employees as instances of Employee and calling the calculateSalary() 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.

To further enhance your understanding of polymorphism and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Why is Twilio calling me?
Why do we select you?
What is unique about Tesla?
What is the salary of risk analyst in PayPal?
What is the goal of Shopify?
What is setState in React?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.