Practice 70 Java Polymorphism Programming Questions
Q1. Write a Java program to demonstrate method overriding using a base class Animal with a method makeSound() and a derived class Dog that overrides the makeSound() method.
Input:
Dog calls makeSound()
Expected Output:
Woof! Woof!
Q2. Write a Java program to demonstrate method overloading with a class Calculator that has multiple add methods to perform addition with different data types (int, double).
Input:
Add two integers: 5 + 7
Add two doubles: 5.5 + 7.2
Expected Output:
Sum (int): 12
Sum (double): 12.7
Q3. Write a Java program to demonstrate method overriding using a Shape class and derived classes Circle and Rectangle. Each class has an area() method that calculates the area.
Input:
Circle with radius 5
Rectangle with length 10 and width 4
Expected Output:
Area of Circle: 78.54
Area of Rectangle: 40
Q4. Write a Java program to demonstrate polymorphism by creating a base class Vehicle with a method move(). Create subclasses Car and Bicycle that override the move() method.
Input:
Call move() for Car and Bicycle
Expected Output:
Car is moving on four wheels
Bicycle is moving on two wheels
Q5. Write a Java program to demonstrate method overloading using a class Display that has multiple show() methods to display different data types.
Input:
Display integer: 5
Display string: "Hello"
Expected Output:
Displaying integer: 5
Displaying string: Hello
Q6. Write a Java program to demonstrate polymorphism using a Payment interface with methods payOnline() and payOffline(). Create classes CreditCardPayment and CashPayment implementing the interface.
Input:
Call payOnline() and payOffline() for each class.
Expected Output:
Credit card payment done online
Cash payment done offline
Q7. Write a Java program to demonstrate method overriding with a base class Bird and derived class Sparrow. Both classes have a method fly().
Input:
Call fly() for Bird and Sparrow
Expected Output:
Bird is flying
Sparrow is flying
Q8. Write a Java program to demonstrate method overloading with a class Printer that has multiple print() methods to print different data types (int, string, and boolean).
Input:
Print an integer: 10
Print a string: "Hello"
Print a boolean: true
Expected Output:
Integer: 10
String: Hello
Boolean: true
Q9. Write a Java program to demonstrate polymorphism using a base class Shape with a method draw(). Create subclasses Circle, Square, and Triangle that override the draw() method.
Input:
Call draw() for each shape
Expected Output:
Drawing Circle
Drawing Square
Drawing Triangle
Q10. Write a Java program to demonstrate polymorphism using a base class Employee with a method salary(). Create subclasses Manager and Engineer that override the salary() method.
Input:
Call salary() for Manager and Engineer
Expected Output:
Manager salary is ₹70000
Engineer salary is ₹50000
Q11. Write a Java program to demonstrate polymorphism using a class Animal with a method speak(). Create subclasses Dog and Cat that override the speak() method.
Input:
Call speak() for Dog and Cat
Expected Output:
Dog says Woof
Cat says Meow
Q12. Write a Java program to demonstrate method overloading with a class MathOperations that has multiple multiply() methods for integers, doubles, and strings.
Input:
Multiply two integers: 3 * 4
Multiply two doubles: 5.5 * 2.2
Expected Output:
Multiplying integers: 12
Multiplying doubles: 12.1
Q13. Write a Java program to demonstrate polymorphism using a base class BankAccount with a method calculateInterest(). Create subclasses SavingsAccount and FixedDepositAccount that override the calculateInterest() method.
Input:
Call calculateInterest() for SavingsAccount and FixedDepositAccount
Expected Output:
Interest for SavingsAccount: ₹500
Interest for FixedDepositAccount: ₹1000
Q14. Write a Java program to demonstrate polymorphism using a base class Appliance with a method turnOn(). Create subclasses Fan and AirConditioner that override the turnOn() method.
Input:
Call turnOn() for Fan and AirConditioner
Expected Output:
Fan is now turned on
AirConditioner is now turned on
Q15. Write a Java program to demonstrate method overloading with a class Calculator that has multiple subtract() methods for integers and floats.
Input:
Subtract two integers: 10 - 5
Subtract two floats: 15.5 - 5.5
Expected Output:
Result (int): 5
Result (float): 10.0
Q16. Write a Java program to demonstrate method overriding using a base class Device with a method turnOn(). Create subclasses Laptop and Smartphone that override the turnOn() method.
Input:
Call turnOn() for Laptop and Smartphone
Expected Output:
Laptop is now on
Smartphone is now on
Q17. Write a Java program to demonstrate polymorphism using a base class Transport with a method travel(). Create subclasses Car and Bus that override the travel() method.
Input:
Call travel() for Car and Bus
Expected Output:
Car is traveling on road
Bus is traveling on road
Q18. Write a Java program to demonstrate method overloading with a class Math that has multiple divide() methods for integers, doubles, and strings.
Input:
Divide two integers: 10 / 5
Divide two doubles: 10.5 / 2.0
Expected Output:
Result (int): 2
Result (double): 5.25
Q19. Write a Java program to demonstrate polymorphism using a base class Product with a method price(). Create subclasses Electronics and Furniture that override the price() method.
Input:
Call price() for Electronics and Furniture
Expected Output:
Price of Electronics: ₹20000
Price of Furniture: ₹5000
Q20. Write a Java program to demonstrate method overriding with a base class Bird and a derived class Penguin. Both classes have a method fly().
Input:
Call fly() for Bird and Penguin
Expected Output:
Bird is flying
Penguin cannot fly
Q21. Write a Java program to demonstrate polymorphism using a class Shape with a method calculateArea(). Create subclasses Square and Circle that override the calculateArea() method.
Input:
Call calculateArea() for Square and Circle
Expected Output:
Area of Square: 25
Area of Circle: 78.54
Q22. Write a Java program to demonstrate method overloading with a class Calculator that has multiple add() methods for integers, doubles, and strings.
Input:
Add two integers: 10 + 5
Add two doubles: 2.5 + 4.5
Expected Output:
Sum of integers: 15
Sum of doubles: 7.0
Q23. Write a Java program to demonstrate polymorphism using a base class Employee with a method work(). Create subclasses Manager and Developer that override the work() method.
Input:
Call work() for Manager and Developer
Expected Output:
Manager is managing tasks
Developer is writing code
Q24. Write a Java program to demonstrate method overriding with a base class Animal and derived classes Dog and Cat. Each class has a method sound().
Input:
Call sound() for Dog and Cat
Expected Output:
Dog barks
Cat meows
Q25. Write a Java program to demonstrate polymorphism by creating a base class Shape with a method area(). Create derived classes Circle and Square that override the area() method.
Input:
Call area() for Circle with radius 7
Call area() for Square with side 4
Expected Output:
Area of Circle: 153.94
Area of Square: 16
Q26. Write a Java program to demonstrate method overloading with a class MathOperation that has a method multiply() for integers and doubles.
Input:
Multiply two integers: 4 * 5
Multiply two doubles: 2.5 * 4.0
Expected Output:
Multiplication of integers: 20
Multiplication of doubles: 10.0
Q27. Write a Java program to demonstrate polymorphism using a base class Person with a method getInfo(). Create subclasses Student and Teacher that override the getInfo() method.
Input:
Call getInfo() for Student and Teacher
Expected Output:
Student: Name - John, Age - 20
Teacher: Name - Mr. Smith, Age - 40
Q28. Write a Java program to demonstrate method overloading with a class Printer that has a method printMessage() for strings and integers.
Input:
Print string message: "Hello"
Print integer message: 100
Expected Output:
Message: Hello
Message: 100
Q29. Write a Java program to demonstrate polymorphism using a class Payment with a method process(). Create subclasses CreditCard and PayPal that override the process() method.
Input:
Call process() for CreditCard and PayPal
Expected Output:
Processing CreditCard payment
Processing PayPal payment
Q30. Write a Java program to demonstrate polymorphism using a class BankAccount with a method calculateFees(). Create subclasses CheckingAccount and SavingsAccount that override the calculateFees() method.
Input:
Call calculateFees() for CheckingAccount and SavingsAccount
Expected Output:
Fees for CheckingAccount: ₹20
Fees for SavingsAccount: ₹10
Q31. Write a Java program to demonstrate method overriding with a base class Vehicle and a derived class Car. Both classes have a method fuelType().
Input:
Call fuelType() for Vehicle and Car
Expected Output:
Vehicle uses Petrol
Car uses Diesel
Q32. Write a Java program to demonstrate method overloading using a class Converter that has multiple convert() methods to convert different data types (e.g., from inches to centimeters and kilograms to pounds).
Input:
Convert inches to centimeters: 5 inches
Convert kilograms to pounds: 3 kg
Expected Output:
5 inches = 12.7 cm
3 kg = 6.61 lbs
Q33. Write a Java program to demonstrate polymorphism using a base class Employee with a method work(). Create subclasses Manager and Salesperson that override the work() method.
Input:
Call work() for Manager and Salesperson
Expected Output:
Manager is managing the team
Salesperson is making sales
Q34. Write a Java program to demonstrate method overriding with a base class Employee and derived class Manager. Both classes have a method salary().
Input:
Call salary() for Employee and Manager
Expected Output:
Employee salary is ₹40000
Manager salary is ₹80000
Q35. Write a Java program to demonstrate method overloading with a class Book that has multiple read() methods for reading books in different formats (e.g., physical, eBook).
Input:
Read a physical book: "Java Programming"
Read an eBook: "Data Structures"
Expected Output:
Reading physical book: Java Programming
Reading eBook: Data Structures
Q36. Write a Java program to demonstrate polymorphism by creating a base class Shape with a method area(). Create subclasses Rectangle and Triangle that override the area() method.
Input:
Call area() for Rectangle with length 5 and width 3
Call area() for Triangle with base 6 and height 4
Expected Output:
Area of Rectangle: 15
Area of Triangle: 12
Q37. Write a Java program to demonstrate method overriding using a base class Appliance and derived class WashingMachine. Both classes have a method operation().
Input:
Call operation() for Appliance and WashingMachine
Expected Output:
Appliance is working
WashingMachine is washing clothes
Q38. Write a Java program to demonstrate polymorphism using a base class Shape with a method draw(). Create subclasses Circle, Square, and Rectangle that override the draw() method.
Input:
Call draw() for Circle, Square, and Rectangle
Expected Output:
Drawing Circle
Drawing Square
Drawing Rectangle
Q39. Write a Java program to demonstrate method overloading using a class MathOperation that has multiple subtract() methods for integers and doubles.
Input:
Subtract two integers: 15 - 7
Subtract two doubles: 9.5 - 4.2
Expected Output:
Result (int): 8
Result (double): 5.3
Q40. Write a Java program to demonstrate polymorphism by creating a class Shape with a method area(). Create derived classes Square and Circle that override the area() method.
Input:
Call area() for Square with side 4
Call area() for Circle with radius 7
Expected Output:
Area of Square: 16
Area of Circle: 153.94
Q41. Write a Java program to demonstrate method overriding with a base class Animal and derived class Dog. Both classes have a method sound().
Input:
Call sound() for Animal and Dog
Expected Output:
Animal makes a sound
Dog barks
Q42. Write a Java program to demonstrate polymorphism by creating a base class Payment with a method processPayment(). Create subclasses CreditCardPayment and CashPayment that override the processPayment() method.
Input:
Call processPayment() for CreditCardPayment and CashPayment
Expected Output:
Processing Credit Card Payment
Processing Cash Payment
Q43. Write a Java program to demonstrate method overloading with a class Printer that has multiple print() methods for integers and strings.
Input:
Print integer: 100
Print string: "Java Programming"
Expected Output:
Printing integer: 100
Printing string: Java Programming
Q44. Write a Java program to demonstrate polymorphism using a base class Shape with a method area(). Create subclasses Circle and Square that override the area() method.
Input:
Call area() for Circle with radius 3
Call area() for Square with side 4
Expected Output:
Area of Circle: 28.27
Area of Square: 16
Q45. Write a Java program to demonstrate polymorphism using a base class Employee with a method work(). Create subclasses Manager and Secretary that override the work() method.
Input:
Call work() for Manager and Secretary
Expected Output:
Manager is managing tasks
Secretary is scheduling meetings
Q46. Write a Java program to demonstrate method overloading using a class DistanceConverter that has multiple convert() methods for converting units (miles to kilometers and pounds to kilograms).
Input:
Convert miles to kilometers: 5 miles
Convert pounds to kilograms: 10 pounds
Expected Output:
5 miles = 8.05 km
10 pounds = 4.54 kg
Q47. Write a Java program to demonstrate method overriding with a base class Transport and derived class Car. Both classes have a method fuelEfficiency().
Input:
Call fuelEfficiency() for Transport and Car
Expected Output:
Transport has average fuel efficiency
Car has high fuel efficiency
Q48. Write a Java program to demonstrate polymorphism using a base class Device with a method powerOn(). Create subclasses Laptop and Smartphone that override the powerOn() method.
Input:
Call powerOn() for Laptop and Smartphone
Expected Output:
Laptop is powered on
Smartphone is powered on
Q49. Write a Java program to demonstrate polymorphism by creating a class Machine with a method start(). Create subclasses WashingMachine and Refrigerator that override the start() method.
Input:
Call start() for WashingMachine and Refrigerator
Expected Output:
WashingMachine started
Refrigerator started
Q50. Write a Java program to demonstrate method overloading with a class Display that has multiple showMessage() methods for different data types (string, integer, and boolean).
Input:
Show string message: "Welcome"
Show integer message: 2025
Expected Output:
Displaying string: Welcome
Displaying integer: 2025
Q51. Write a Java program to demonstrate polymorphism by creating a base class BankAccount with a method calculateBalance(). Create subclasses SavingsAccount and CurrentAccount that override the calculateBalance() method.
Input:
Call calculateBalance() for SavingsAccount and CurrentAccount
Expected Output:
Balance in SavingsAccount: ₹5000
Balance in CurrentAccount: ₹2000
Q52. Write a Java program to demonstrate polymorphism using a base class Shape with a method draw(). Create subclasses Rectangle and Circle that override the draw() method.
Input:
Call draw() for Rectangle and Circle
Expected Output:
Drawing Rectangle
Drawing Circle
Q53. Write a Java program to demonstrate method overloading with a class Rectangle that has multiple calculateArea() methods for different input types (length and width, radius).
Input:
Calculate area of rectangle with length 5 and width 4
Calculate area of circle with radius 3
Expected Output:
Area of Rectangle: 20
Area of Circle: 28.27
Q54. Write a Java program to demonstrate polymorphism by creating a class Shape with a method calculateArea(). Create subclasses Square and Circle that override the calculateArea() method.
Input:
Call calculateArea() for Square with side 6
Call calculateArea() for Circle with radius 5
Expected Output:
Area of Square: 36
Area of Circle: 78.54
Q55. Write a Java program to demonstrate polymorphism by creating a base class Transport with a method move(). Create subclasses Car and Bicycle that override the move() method.
Input:
Call move() for Car and Bicycle
Expected Output:
Car moves on four wheels
Bicycle moves on two wheels
Q56. Write a Java program to demonstrate method overloading using a class AreaCalculator that has multiple calculate() methods for calculating the area of a square, rectangle, and circle.
Input:
Calculate area of square with side 4
Calculate area of rectangle with length 4 and width 5
Calculate area of circle with radius 3
Expected Output:
Area of Square: 16
Area of Rectangle: 20
Area of Circle: 28.27
Q57. Write a Java program to demonstrate polymorphism using a base class Employee with a method getDetails(). Create subclasses Manager and Developer that override the getDetails() method.
Input:
Call getDetails() for Manager and Developer
Expected Output:
Manager: Name - John, Role - Manager
Developer: Name - Alice, Role - Developer
Q58. Write a Java program to demonstrate polymorphism by creating a base class Instrument with a method play(). Create subclasses Guitar and Drums that override the play() method.
Input:
Call play() for Guitar and Drums
Expected Output:
Playing Guitar
Playing Drums
Q59. Write a Java program to demonstrate polymorphism by creating a base class Vehicle with a method move(). Create subclasses Truck and Bus that override the move() method.
Input:
Call move() for Truck and Bus
Expected Output:
Truck moves on the road
Bus moves on the road
Q60. Write a Java program to demonstrate method overloading with a class DisplayMessage that has multiple show() methods for displaying messages of different data types.
Input:
Display message: "Welcome"
Display number: 1234
Expected Output:
Displaying message: Welcome
Displaying number: 1234
Q61. Write a Java program to demonstrate method overriding with a base class Animal and derived class Lion. Both classes have a method sound().
Input:
Call sound() for Animal and Lion
Expected Output:
Animal makes a sound
Lion roars
Q62. Write a Java program to demonstrate polymorphism using a base class Person with a method showInfo(). Create subclasses Student and Teacher that override the showInfo() method.
Input:
Call showInfo() for Student and Teacher
Expected Output:
Student: Name - Alice, Age - 21
Teacher: Name - Mr. Brown, Age - 45
Q63. Write a Java program to demonstrate polymorphism by creating a base class Employee with a method work(). Create subclasses Intern and Manager that override the work() method.
Input:
Call work() for Intern and Manager
Expected Output:
Intern is learning
Manager is overseeing the project
Q64. Write a Java program to demonstrate method overloading with a class Math that has a multiply() method for integers, doubles, and floats.
Input:
Multiply two integers: 5 * 2
Multiply two doubles: 3.2 * 4.1
Expected Output:
Multiplication of integers: 10
Multiplication of doubles: 13.12
Q65. Write a Java program to demonstrate method overriding using a base class Transport and derived class Bike. Both classes have a method speed().
Input:
Call speed() for Transport and Bike
Expected Output:
Transport speed: 60 km/h
Bike speed: 80 km/h
Q66. Write a Java program to demonstrate polymorphism using a base class Shape with a method area(). Create subclasses Rectangle and Circle that override the area() method.
Input:
Call area() for Rectangle with length 7 and width 5
Call area() for Circle with radius 3
Expected Output:
Area of Rectangle: 35
Area of Circle: 28.27
Q67. Write a Java program to demonstrate method overloading using a class Calculator that has divide() methods for integers, floats, and doubles.
Input:
Divide two integers: 10 / 2
Divide two floats: 5.5 / 2.5
Expected Output:
Division of integers: 5
Division of floats: 2.2
Q68. Write a Java program to demonstrate method overriding with a base class Vehicle and derived class Motorcycle. Both classes have a method horn().
Input:
Call horn() for Vehicle and Motorcycle
Expected Output:
Vehicle horn sound
Motorcycle horn sound
Q69. Write a Java program to demonstrate polymorphism by creating a base class Animal with a method makeSound(). Create subclasses Dog and Cat that override the makeSound() method.
Input:
Call makeSound() for Dog and Cat
Expected Output:
Dog barks
Cat meows
Q70. Write a Java program to demonstrate polymorphism using a base class ElectronicDevice with a method turnOn(). Create subclasses TV and Radio that override the turnOn() method.
Input:
Call turnOn() for TV and Radio
Expected Output:
Turning on TV
Turning on Radio
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!