Practice 70 Java Abstract Classes Programming Questions
Q1. Write a Java program to create an abstract class Shape with an abstract method calculateArea(). Create two subclasses, Circle and Rectangle, and calculate the area of a circle with radius 5 and a rectangle with width 4 and height 6.
Input:
Radius: 5
Width: 4
Height: 6
Expected Output:
Area of Circle: 78.54
Area of Rectangle: 24
Q2. Write a Java program to create an abstract class Animal with an abstract method sound(). Implement this method in subclasses Dog and Cat to display respective sounds.
Input: None
Expected Output:
Dog: Woof
Cat: Meow
Q3. Write a Java program to create an abstract class Vehicle with an abstract method move(). Create subclasses Car and Bike and override move() to print specific movements.
Input: None
Expected Output:
Car is moving on four wheels.
Bike is moving on two wheels.
Q4. Write a Java program to create an abstract class Account with an abstract method calculateInterest(). Create subclasses SavingsAccount and CurrentAccount. Calculate interest for a savings account with balance 10000 and a rate of 5%.
Input:
Balance: 10000
Rate: 5%
Expected Output:
Interest: 500
Q5. Write a Java program to create an abstract class Employee with an abstract method calculateSalary(). Create subclasses Manager and Developer and calculate salaries with respective bonuses.
Input:
Base Salary: 30000 for Manager
Base Salary: 25000 for Developer
Bonus: 5000 for Manager, 3000 for Developer
Expected Output:
Manager's Total Salary: 35000
Developer's Total Salary: 28000
Q6. Write a Java program to create an abstract class Appliance with an abstract method getPowerConsumption(). Implement subclasses WashingMachine and Refrigerator to calculate power consumption.
Input:
WashingMachine Power: 500W for 2 hours
Refrigerator Power: 150W for 24 hours
Expected Output:
WashingMachine: 1000W
Refrigerator: 3600W
Q7. Write a Java program to create an abstract class Food with an abstract method getCalories(). Implement subclasses Pizza and Salad to calculate respective calories.
Input:
Pizza: 300 calories per slice, 3 slices
Salad: 150 calories per bowl, 2 bowls
Expected Output:
Pizza: 900 calories
Salad: 300 calories
Q8. Write a Java program to create an abstract class Shape with an abstract method calculatePerimeter(). Create subclasses Triangle and Square to calculate the perimeter.
Input:
Triangle sides: 3, 4, 5
Square side: 4
Expected Output:
Triangle Perimeter: 12
Square Perimeter: 16
Q9. Write a Java program to create an abstract class Device with an abstract method getSpecification(). Implement subclasses Mobile and Laptop to display specifications.
Input: None
Expected Output:
Mobile: 6GB RAM, 128GB Storage
Laptop: 16GB RAM, 1TB Storage
Q10. Write a Java program to create an abstract class Game with an abstract method play(). Create subclasses Cricket and Football to display gameplay.
Input: None
Expected Output:
Playing Cricket
Playing Football
Q11. Write a Java program to create an abstract class Instrument with an abstract method playSound(). Implement subclasses Guitar and Piano to simulate sound output.
Input: None
Expected Output:
Guitar sound: Strum
Piano sound: Ding
Q12. Write a Java program to create an abstract class Transport with an abstract method fuelEfficiency(). Implement subclasses Bus and Train to calculate fuel efficiency.
Input:
Bus: 10 km/l for 100 km
Train: 20 km/l for 200 km
Expected Output:
Bus Fuel Used: 10 liters
Train Fuel Used: 10 liters
Q13. Write a Java program to create an abstract class Appliance with an abstract method getWarranty(). Implement subclasses TV and Microwave to display warranty details.
Input: None
Expected Output:
TV Warranty: 2 years
Microwave Warranty: 1 year
Q14. Write a Java program to create an abstract class Person with an abstract method getDetails(). Create subclasses Student and Teacher to display respective details.
Input: None
Expected Output:
Student: Name: John, Grade: A
Teacher: Name: Sarah, Subject: Math
Q15. Write a Java program to create an abstract class Animal with an abstract method eat(). Implement subclasses Herbivore and Carnivore to define specific eating habits.
Input: None
Expected Output:
Herbivore: Eating plants.
Carnivore: Eating meat.
Q16. Write a Java program to create an abstract class Bank with an abstract method getInterestRate(). Implement subclasses HDFC and SBI to display interest rates.
Input: None
Expected Output:
HDFC Interest Rate: 6.5%
SBI Interest Rate: 5.5%
Q17. Write a Java program to create an abstract class Game with an abstract method start(). Create subclasses Chess and Carrom to display the start message.
Input: None
Expected Output:
Starting Chess...
Starting Carrom...
Q18. Write a Java program to create an abstract class Payment with an abstract method processPayment(). Implement subclasses CreditCard and PayPal to process payments.
Input: None
Expected Output:
Payment processed via Credit Card.
Payment processed via PayPal.
Q19. Write a Java program to create an abstract class Vehicle with an abstract method fuelType(). Implement subclasses ElectricCar and DieselTruck to display fuel type.
Input: None
Expected Output:
ElectricCar: Electricity
DieselTruck: Diesel
Q20. Write a Java program to create an abstract class Shape with an abstract method getType(). Implement subclasses Polygon and Ellipse to display the type.
Input: None
Expected Output:
Polygon: Has straight sides.
Ellipse: Has curved sides.
Q21. Write a Java program to create an abstract class Order with an abstract method calculateTotal(). Create subclasses OnlineOrder and StoreOrder to calculate total amounts.
Input:
OnlineOrder: Item Price = 100, Quantity = 2, Shipping = 20
StoreOrder: Item Price = 80, Quantity = 3
Expected Output:
OnlineOrder Total: 220
StoreOrder Total: 240
Q22. Write a Java program to create an abstract class Browser with an abstract method renderPage(). Implement subclasses Chrome and Firefox to simulate rendering.
Input: None
Expected Output:
Rendering page in Chrome...
Rendering page in Firefox...
Q23. Write a Java program to create an abstract class Document with an abstract method printDetails(). Implement subclasses PDF and Word to print document details.
Input: None
Expected Output:
PDF Document: Portable File
Word Document: Editable File
Q24. Write a Java program to create an abstract class Pet with an abstract method performAction(). Implement subclasses Dog and Parrot to display actions.
Input: None
Expected Output:
Dog: Fetching a stick.
Parrot: Talking to humans.
Q25. Write a Java program to create an abstract class Vehicle with an abstract method getSpeed(). Implement subclasses Train and Plane to display speeds.
Input: None
Expected Output:
Train Speed: 100 km/h
Plane Speed: 800 km/h
Q26.Write a Java program to create an abstract class Shape with an abstract method draw(). Implement subclasses Line and Circle to draw respective shapes.
Input: None
Expected Output:
Drawing a Line
Drawing a Circle
Q27. Write a Java program to create an abstract class Device with an abstract method getDetails(). Implement subclasses Smartphone and Tablet to display device details.
Input: None
Expected Output:
Smartphone: 8GB RAM, 128GB Storage
Tablet: 4GB RAM, 64GB Storage
Q28. Write a Java program to create an abstract class Appliance with an abstract method getCost(). Implement subclasses Fan and Heater to calculate and display costs.
Input:
Fan: 1200 INR
Heater: 2500 INR
Expected Output:
Fan Cost: 1200 INR
Heater Cost: 2500 INR
Q29. Write a Java program to create an abstract class Employee with an abstract method getRole(). Implement subclasses Engineer and Designer to display roles.
Input: None
Expected Output:
Engineer: Responsible for development.
Designer: Responsible for creativity.
Q30. Write a Java program to create an abstract class Account with an abstract method withdraw(). Implement subclasses SavingsAccount and FixedDeposit to handle respective withdrawals.
Input:
SavingsAccount: Balance = 20000, Withdraw = 5000
FixedDeposit: Balance = 100000, Withdraw = 20000
Expected Output:
SavingsAccount Balance: 15000
FixedDeposit Balance: 80000
Q31. Write a Java program to create an abstract class Transport with an abstract method speed(). Implement subclasses Aeroplane and Ship to display respective speeds.
Input: None
Expected Output:
Aeroplane: Speed 900 km/h
Ship: Speed 60 km/h
Q32. Write a Java program to create an abstract class Game with an abstract method rules(). Implement subclasses Basketball and Tennis to display game rules.
Input: None
Expected Output:
Basketball Rules: 5 players per team.
Tennis Rules: Played in sets.
Q33. Write a Java program to create an abstract class Shape with an abstract method getColor(). Implement subclasses Square and Circle to return colors.
Input: None
Expected Output:
Square Color: Red
Circle Color: Blue
Q34. Write a Java program to create an abstract class Furniture with an abstract method materialUsed(). Implement subclasses Chair and Table to display materials used.
Input: None
Expected Output:
Chair: Wood
Table: Metal
Q35. Write a Java program to create an abstract class Shop with an abstract method getProduct(). Implement subclasses ClothingStore and ElectronicsStore to list products.
Input: None
Expected Output:
ClothingStore: Shirts, Pants
ElectronicsStore: Mobiles, Laptops
Q36. Write a Java program to create an abstract class Food with an abstract method getIngredients(). Implement subclasses Cake and Sandwich to list respective ingredients.
Input: None
Expected Output:
Cake Ingredients: Flour, Sugar, Eggs
Sandwich Ingredients: Bread, Lettuce, Cheese
Q37. Write a Java program to create an abstract class Bank with an abstract method createAccount(). Implement subclasses PrivateBank and PublicBank to create accounts.
Input: None
Expected Output:
PrivateBank Account Created
PublicBank Account Created
Q38. Write a Java program to create an abstract class Transport with an abstract method capacity(). Implement subclasses Train and Bus to display seating capacity.
Input: None
Expected Output:
Train: 1000 seats
Bus: 50 seats
Q39. Write a Java program to create an abstract class Product with an abstract method calculatePrice(). Implement subclasses Electronics and Grocery to calculate prices.
Input:
Electronics: Price = 5000
Grocery: Price = 1000
Expected Output:
Electronics Total Price: 5000
Grocery Total Price: 1000
Q40. Write a Java program to create an abstract class Job with an abstract method getResponsibility(). Implement subclasses Developer and Tester to display respective responsibilities.
Input: None
Expected Output:
Developer: Writing code.
Tester: Testing code.
Q41. Write a Java program to create an abstract class Animal with an abstract method getLifespan(). Implement subclasses Elephant and Turtle to calculate their lifespan.
Input: None
Expected Output:
Elephant Lifespan: 60-70 years
Turtle Lifespan: 80-150 years
Q42. Write a Java program to create an abstract class Shape with an abstract method calculateArea(). Implement a subclass Ellipse to calculate its area.
Input:
Semi-major axis: 6
Semi-minor axis: 4
Expected Output:
Area of Ellipse: 75.36
Q43. Write a Java program to create an abstract class Bank with an abstract method deposit(). Implement subclasses HDFC and ICICI to deposit money and display balance.
Input:
HDFC: Deposit 10000
ICICI: Deposit 5000
Expected Output:
HDFC Balance: 10000
ICICI Balance: 5000
Q44. Write a Java program to create an abstract class Person with an abstract method getIncome(). Implement subclasses Doctor and Artist to calculate and display income.
Input:
Doctor: Fixed salary = 80000, Bonus = 20000
Artist: Per artwork = 15000, Artworks sold = 5
Expected Output:
Doctor Income: 100000
Artist Income: 75000
Q45. Write a Java program to create an abstract class Vehicle with an abstract method getMaxSpeed(). Implement subclasses SUV and SportsCar to display maximum speeds.
Input: None
Expected Output:
SUV Max Speed: 180 km/h
SportsCar Max Speed: 300 km/h
Q46. Write a Java program to create an abstract class Device with an abstract method batteryLife(). Implement subclasses Laptop and Tablet to display battery life.
Input: None
Expected Output:
Laptop Battery Life: 6 hours
Tablet Battery Life: 10 hours
Q47. Write a Java program to create an abstract class Food with an abstract method getCalories(). Implement subclasses Burger and Smoothie to calculate and display calories.
Input:
Burger: 250 calories per patty, 2 patties
Smoothie: 200 calories per glass, 2 glasses
Expected Output:
Burger Calories: 500
Smoothie Calories: 400
Q48. Write a Java program to create an abstract class Payment with an abstract method processPayment(). Implement subclasses UPI and Cash to process payments and display messages.
Input: None
Expected Output:
Payment processed via UPI.
Payment processed in Cash.
Q49. Write a Java program to create an abstract class Game with an abstract method getRules(). Implement subclasses Hockey and Badminton to display their rules.
Input: None
Expected Output:
Hockey Rules: 11 players per team, two halves of 35 minutes.
Badminton Rules: Played in sets of 21 points each.
Q50. Write a Java program to create an abstract class Furniture with an abstract method material(). Implement subclasses Couch and Bookshelf to display materials used.
Input: None
Expected Output:
Couch: Leather
Bookshelf: Wood
Q51. Write a Java program to create an abstract class Appliance with an abstract method getRuntime(). Implement subclasses AirConditioner and VacuumCleaner to calculate runtime.
Input:
AirConditioner: 5 hours daily for 30 days
VacuumCleaner: 2 hours weekly for 4 weeks
Expected Output:
AirConditioner Runtime: 150 hours
VacuumCleaner Runtime: 8 hours
Q52. Write a Java program to create an abstract class Employee with an abstract method getPerks(). Implement subclasses HR and Admin to calculate perks.
Input:
HR: Basic salary = 50000, Perks = 20%
Admin: Basic salary = 40000, Perks = 15%
Expected Output:
HR Total Salary: 60000
Admin Total Salary: 46000
Q53. Write a Java program to create an abstract class Browser with an abstract method getDataUsage(). Implement subclasses Edge and Opera to calculate data usage.
Input:
Edge: 500MB
Opera: 300MB
Expected Output:
Edge Data Usage: 500MB
Opera Data Usage: 300MB
Q54. Write a Java program to create an abstract class Shop with an abstract method applyDiscount(). Implement subclasses Mall and StreetVendor to calculate total after discounts.
Input:
Mall: Item Price = 1000, Discount = 10%
StreetVendor: Item Price = 500, Discount = 5%
Expected Output:
Mall Total: 900
StreetVendor Total: 475
Q55. Write a Java program to create an abstract class Shape with an abstract method getDimensions(). Implement subclasses Trapezium and Rhombus to calculate areas.
Input:
Trapezium: Bases = 6, 4, Height = 5
Rhombus: Diagonals = 8, 6
Expected Output:
Trapezium Area: 25
Rhombus Area: 24
Q56. Write a Java program to create an abstract class Pet with an abstract method feedingSchedule(). Implement subclasses Fish and Rabbit to display schedules.
Input: None
Expected Output:
Fish: Feed twice a day.
Rabbit: Feed thrice a day.
Q57. Write a Java program to create an abstract class Account with an abstract method performTransaction(). Implement subclasses ForexAccount and CryptoAccount to simulate transactions.
Input: None
Expected Output:
Forex Transaction: 500 USD to EUR
Crypto Transaction: 0.01 BTC
Q58. Write a Java program to create an abstract class Vehicle with an abstract method getGearSystem(). Implement subclasses Truck and Scooter to display gear systems.
Input: None
Expected Output:
Truck: Manual Gear System
Scooter: Automatic Gear System
Q59. Write a Java program to create an abstract class Document with an abstract method getVersion(). Implement subclasses PDF and Spreadsheet to display versions.
Input: None
Expected Output:
PDF Version: 1.4
Spreadsheet Version: Excel 2020
Q60. Write a Java program to create an abstract class Instrument with an abstract method getSoundType(). Implement subclasses Flute and Drum to display sound types.
Input: None
Expected Output:
Flute: Melodious
Drum: Percussive
Q61. Write a Java program to create an abstract class Food with an abstract method checkFreshness(). Implement subclasses Fruit and Vegetable to display freshness.
Input:
Fruit: Purchased 2 days ago
Vegetable: Purchased 5 days ago
Expected Output:
Fruit: Fresh
Vegetable: Not Fresh
Q62. Write a Java program to create an abstract class Bank with an abstract method getSavingsPlan(). Implement subclasses PostOffice and CooperativeBank to display savings plans.
Input: None
Expected Output:
PostOffice: Interest Rate = 4%
CooperativeBank: Interest Rate = 5%
Q63. Write a Java program to create an abstract class Transport with an abstract method calculatePollutionLevel(). Implement subclasses Car and Boat to display pollution levels.
Input:
Car: Fuel = Petrol
Boat: Fuel = Diesel
Expected Output:
Car Pollution Level: Moderate
Boat Pollution Level: High
Q64. Write a Java program to create an abstract class Game with an abstract method gameDuration(). Implement subclasses Chess and Racing to calculate game durations.
Input:
Chess: 90 minutes
Racing: 120 minutes
Expected Output:
Chess Duration: 90 minutes
Racing Duration: 120 minutes
Q65. Write a Java program to create an abstract class Furniture with an abstract method calculateWeight(). Implement subclasses Bed and Wardrobe to calculate and display weights.
Input:
Bed: 100kg
Wardrobe: 150kg
Expected Output:
Bed Weight: 100kg
Wardrobe Weight: 150kg
Q66. Write a Java program to create an abstract class Animal with an abstract method getHabitat(). Implement subclasses PolarBear and Camel to display their respective habitats.
Input: None
Expected Output:
PolarBear Habitat: Arctic Region
Camel Habitat: Desert
Q67. Write a Java program to create an abstract class Device with an abstract method repair(). Implement subclasses Phone and Printer to simulate repair processes.
Input: None
Expected Output:
Phone: Repaired at Service Center
Printer: Repaired by Technician
Q68. Write a Java program to create an abstract class Appliance with an abstract method calculatePowerCost(). Implement subclasses Iron and Kettle to calculate power costs based on usage.
Input:
Iron: 2 hours daily, 30 days, 1000W
Kettle: 1 hour daily, 30 days, 1500W
Expected Output:
Iron Power Cost: 60 units
Kettle Power Cost: 45 units
Q69. Write a Java program to create an abstract class Employee with an abstract method assignProject(). Implement subclasses Developer and Manager to assign projects.
Input: None
Expected Output:
Developer: Assigned a coding project.
Manager: Assigned a team management project.
Q70. Write a Java program to create an abstract class Vehicle with an abstract method fuelEfficiency(). Implement subclasses ElectricBike and HybridCar to calculate and display fuel efficiency.
Input:
ElectricBike: 100km per charge
HybridCar: 25km per liter of fuel
Expected Output:
ElectricBike Fuel Efficiency: 100km/charge
HybridCar Fuel Efficiency: 25km/liter
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!