Pages

About Us

 About Us

Polymorphism


Poly means many and morphism means forms so Polymorphism is the ability of a variable, object, or function to take on multiple forms(for example A man is father, brother, worker, etc)
Benefits of Polymorphism: 
  • Code Reusability:
  • Flexibility:

There are 2 types of Polymorphism
  1. Compile-Time Polymorphism (Method Overloading).
  2. Run-Time Polymorphism (Method Overriding).

Practice Task:

Compile-Time Polymorphism (Method Overloading).

Q1:Create a class Calculator with overloaded methods to perform addition, subtraction, multiplication, and division for different numeric types (int, double). Demonstrate the usage of these methods.
Implement a class MathUtility with overloaded methods to calculate the square of an integer, the square of a double, and the cube of an integer. Display the results of these calculations.

Run-Time Polymorphism (Method Overriding).

Q1:Create a base class Shape with a method area() and then create subclasses like Circle, Rectangle, and Triangle that overrides the area() method. Demonstrate polymorphism by calculating and displaying the areas of different shapes.
Q2:Design a base class Vehicle with methods start() and stop(), and then create subclasses like Car, Motorcycle, and Bus that override these methods. Show how polymorphism allows you to start and stop different vehicles seamlessly.
Q3:Develop a base class Employee with a method calculateSalary(), and then create subclasses like Manager, Engineer, and Salesperson that override this method. Use polymorphism to calculate and display the salaries of different employees.
Q4:Design a base class Animal with methods like makeSound() and move(), and then create subclasses like Dog, Cat, and Bird that override these methods. Showcase how polymorphism allows different animals to make sounds and move uniquely.
Q5:Create a base class Payment with a method processPayment() and subclasses like CreditCard, PayPal, and BankTransfer that override this method. Use polymorphism to process different payment methods.
Q6:Design a base class MediaPlayer with methods like play() and stop(), and then create subclasses like AudioPlayer, VideoPlayer, and ImagePlayer that override these methods. Demonstrate how polymorphism enables different types of media to be played and stopped.
Q7:Extend the employee hierarchy (e.g., Employee, Manager, Engineer) with a new method, such as displayInfo(), and demonstrate polymorphism by displaying information for different types of employees. 
Q8:Create a base class Product with methods like calculatePrice() and displayInfo(), and then create subclasses like Electronics, Clothing, and Books that override these methods. Use polymorphism to calculate prices and display info for different product types. 
Q9:Develop a program to draw different shapes (e.g., circles, squares) using polymorphism. Implement a base class Shape with a method draw(), and then create subclasses for each shape type. 
Q10:Design a system for an online store with different types of products (e.g., electronics, books). Create a base class Product with methods like getDescription() and getPrice(), and then create subclasses for each product category to demonstrate polymorphism.

No comments:

Post a Comment