Lab 7
Lab 7
1. What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. In Java, it allows
a single interface to be used for a general class of actions. The most common use
of polymorphism is when a parent class reference is used to refer to a child class
object.
@Override
public void eat() {
System.out.println("Dog eats meat");
}
}
@Override
public void eat() {
System.out.println("Cat eats fish");
}
}
4. Activities
🔷 Method Overloading – Activity Questions
5. Lab Tasks
Task 1: Shape Hierarchy
Create a base class Shape with a method calculateArea(). Create subclasses Circle,
Rectangle, and Triangle that override the calculateArea() method with their specific area
calculation formulas. Test using a Shape reference to call the overridden methods.
Task 2: Banking System
Create a base class BankAccount with a method calculateInterest(). Create subclasses
SavingsAccount and FixedDepositAccount that override the method with different interest
calculation logic. Demonstrate using a BankAccount reference.
Task 3: File Handling
Create a base class FileHandler with a method processFile(). Create subclasses
TextFileHandler and BinaryFileHandler that override the method with different file
processing implementations. Test with a FileHandler reference.
Task 4: Notification System
Create a base class Notification with a method send(). Create subclasses EmailNotification,
SMSNotification, and PushNotification that override the method with their specific sending
mechanisms. Demonstrate polymorphism using a Notification reference.
Task 5: Transportation
Create a base class Transport with methods move() and fuelConsumption(). Create
subclasses Car, Bicycle, and Airplane that override these methods. Test using a Transport
reference.
Task 6: String Manipulation
Create a class StringFormatter with overloaded methods format() that accept different
parameter combinations:
● A string only (returns uppercase)
● A string and boolean (if true returns uppercase, else lowercase)
● A string and an integer (repeats the string n times)
● A string, integer, and char (repeats the string n times with the char as separator)
Task 7: Geometric Calculation
Create a class GeometryCalculator with overloaded methods calculateArea() that:
● Accept a single integer (calculates area of a square)
● Accept two integers (calculates area of a rectangle)
● Accept a single double (calculates area of a circle)
● Accept three doubles (calculates area of a triangle using Heron's formula)
Task 8: Data Converter
Create a class DataConverter with overloaded methods convert() that:
● Accept an integer array (returns a double array)
● Accept a double array (returns an integer array)
● Accept a string array (returns an integer array by parsing)
● Accept a string and a format pattern (parses string to date)
Task 9: Print Utility
Create a class PrintUtil with overloaded methods print() that:
● Accept a string (prints as is)
● Accept an array (prints all elements)
● Accept a collection (prints all elements)
● Accept an object and boolean (prints object details if boolean is true)
Task 10: Math Operations
Create a class MathOperations with overloaded methods max() that:
● Accept two integers (returns the larger one)
● Accept three integers (returns the largest)
● Accept an integer array (returns the maximum value)
● Accept a list of integers (returns the maximum value)