What is an example of a polymorphism?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Which IT field is best without coding?
Are meta interviews hard?
What is the minimum salary in Adobe?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.