1. Write a program to create an abstract class Shape with an abstract method area(). Then, create two subclasses: Circle and Rectangle, both of which override the area() method to calculate the area of the respective shapes?
| abstract class Shape { abstract void area(); } class Circle extends Shape { double radius; Circle(double radius) { this.radius = radius; } @Override void area() { System.out.println(“Area of Circle: ” + (3.14 * radius * radius)); } } class Rectangle extends Shape { double length, width; Rectangle(double length, double width) { this.length = length; this.width = width; } @Override void area() { System.out.println(“Area of Rectangle: ” + (length * width)); } } public class Main { public static void main(String[] args) { Shape shape1 = new Circle(5); shape1.area(); Shape shape2 = new Rectangle(4, 6); shape2.area(); } } |
Output :
| Area of Circle: 78.5 Area of Rectangle: 24.0 |
2. Write a program to design an abstract class Employee with an abstract method calculateSalary() then, create two subclasses: Manager and Developer, which calculate salary in different ways ?
| abstract class Employee { abstract void calculateSalary(); } class Manager extends Employee { @Override void calculateSalary() { System.out.println(“Manager’s salary is calculated with a fixed amount.”); } } class Developer extends Employee { @Override void calculateSalary() { System.out.println(“Developer’s salary is calculated based on hourly rate.”); } } public class Main { public static void main(String[] args) { Employee emp1 = new Manager(); emp1.calculateSalary(); Employee emp2 = new Developer(); emp2.calculateSalary(); } } |
Output :
| Manager’s salary is calculated with a fixed amount. Developer’s salary is calculated based on hourly rate. |
3. Write a program to design an abstract class Account with an abstract method deposit(). Create subclasses SavingsAccount and CurrentAccount that implement the deposit method with different deposit logic?
| abstract class Account { abstract void deposit(); } class SavingsAccount extends Account { @Override void deposit() { System.out.println(“Deposit to Savings Account.”); } } class CurrentAccount extends Account { @Override void deposit() { System.out.println(“Deposit to Current Account.”); } } public class Main { public static void main(String[] args) { Account acc1 = new SavingsAccount(); acc1.deposit(); Account acc2 = new CurrentAccount(); acc2.deposit(); } } |
Output :
| Deposit to Savings Account. Deposit to Current Account. |
4. Write a program to create an abstract class Appliance with an abstract method turnOn(). Then, create two subclasses: WashingMachine and Refrigerator that implement the turnOn() method with specific behavior?
| abstract class Appliance { abstract void turnOn(); } class WashingMachine extends Appliance { @Override void turnOn() { System.out.println(“Washing machine is now ON.”); } } class Refrigerator extends Appliance { @Override void turnOn() { System.out.println(“Refrigerator is now ON.”); } } public class Main { public static void main(String[] args) { Appliance appliance1 = new WashingMachine(); appliance1.turnOn(); Appliance appliance2 = new Refrigerator(); appliance2.turnOn(); } } |
Output :
| Washing machine is now ON. Refrigerator is now ON. |
5. Write a program to create an abstract class Animal with an abstract method makeSound(). Then, create two subclasses: Dog and Cat which override the makeSound() method to print specific sounds. Demonstrate abstraction by calling the makeSound() method on different objects?
| abstract class Animal { abstract void makeSound(); } class Dog extends Animal { @Override void makeSound() { System.out.println(“Dog barks.”); } } class Cat extends Animal { @Override void makeSound() { System.out.println(“Cat meows.”); } } public class Main { public static void main(String[] args) { Animal animal1 = new Dog(); animal1.makeSound(); Animal animal2 = new Cat(); animal2.makeSound(); } } |
Output :
| Dog barks. Cat meows. |
6. Write a program to create an abstract class Shape with an abstract method draw(). Then, create two subclasses: Circle and Square that implement the draw() method to print a shape-specific message. Write a program to demonstrate how abstraction is used to draw different shapes?
| abstract class Shape { abstract void draw(); } class Circle extends Shape { @Override void draw() { System.out.println(“Drawing a Circle.”); } } class Square extends Shape { @Override void draw() { System.out.println(“Drawing a Square.”); } } public class Main { public static void main(String[] args) { Shape shape1 = new Circle(); shape1.draw(); Shape shape2 = new Square(); shape2.draw(); } } |
Output :
| Drawing a Circle. Drawing a Square. |
7. Write a program to create an abstract class Vehicle with an abstract method fuelType(). Then, create two subclasses: ElectricCar and GasCar, which implement fuelType() to return different types of fuel?
| abstract class Vehicle { abstract void fuelType(); } class ElectricCar extends Vehicle { @Override void fuelType() { System.out.println(“Electric Car uses electricity.”); } } class GasCar extends Vehicle { @Override void fuelType() { System.out.println(“Gas Car uses gasoline.”); } } public class Main { public static void main(String[] args) { Vehicle vehicle1 = new ElectricCar(); vehicle1.fuelType(); Vehicle vehicle2 = new GasCar(); vehicle2.fuelType(); } } |
Output :
| Electric Car uses electricity. Gas Car uses gasoline. |
8. Write a Java program where you have an abstract class Person with an abstract method introduce(). Create two subclasses Student and Teacher, where the introduce() method introduces themselves differently?
| abstract class Person { abstract void introduce(); } class Student extends Person { @Override void introduce() { System.out.println(“I am a Student.”); } } class Teacher extends Person { @Override void introduce() { System.out.println(“I am a Teacher.”); } } public class Main { public static void main(String[] args) { Person person1 = new Student(); person1.introduce(); Person person2 = new Teacher(); person2.introduce(); } } |
Output :
| I am a Student. I am a Teacher. |
9. Write a program to create an abstract class Building with an abstract method constructionCost() then, create two subclasses House and Office that implement constructionCost() to calculate costs differently based on building type?
| abstract class Building { abstract void constructionCost(); } class House extends Building { @Override void constructionCost() { double cost = 100000; System.out.println(“Construction cost for House: $” + cost); } } class Office extends Building { @Override void constructionCost() { double cost = 500000; System.out.println(“Construction cost for Office: $” + cost); } } public class Main { public static void main(String[] args) { // Create instances of House and Office Building building1 = new House(); building1.constructionCost(); Building building2 = new Office(); building2.constructionCost(); } } |
Output :
| Construction cost for House: $100000.0 Construction cost for Office: $500000.0 |
