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?
Algorithm :
- Start the program.
- Define an abstract class Shape with an abstract method area().
- Create a subclass Circle:
- Add a radius variable.
- Define a constructor to initialize radius.
- Override the area() method to calculate and print the area of the circle using the formula πr².
- Create another subclass Rectangle:
- Add length and width variables.
- Define a constructor to initialize them.
- Override the area() method to calculate and print the area as length × width.
- In the Main class:
- Create a Shape reference to a Circle object and call its area() method.
- Create another Shape reference to a Rectangle object and call its area() method.
- End the program.
Program :
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 |
Explanation :
This program demonstrates the use of abstract classes and runtime polymorphism in Java. The abstract class Shape defines an abstract method area(), which is implemented in both Circle and Rectangle subclasses with their specific area formulas. In the main method, Shape references are used to point to Circle and Rectangle objects. When area() is called, the appropriate overridden method is executed based on the actual object, showing how Java enables polymorphic behavior using abstract classes. |
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 ?
Algorithm :
- Start the program.
- Define an abstract class Employee with an abstract method calculateSalary().
- Create a subclass Manager:
- Override calculateSalary() to print a message indicating fixed salary calculation.
- Create another subclass Developer:
- Override calculateSalary() to print a message indicating salary based on hourly rate.
- In the Main class:
- Create an Employee reference emp1 pointing to a Manager object and call calculateSalary().
- Create another Employee reference emp2 pointing to a Developer object and call calculateSalary().
- End the program.
Program :
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. |
Explanation :
This program illustrates abstraction and runtime polymorphism in Java. The abstract class Employee provides a general method calculateSalary(), which is implemented differently in Manager and Developer subclasses. The main method creates Employee references for both types and calls their respective calculateSalary() methods. Java dynamically binds the correct method at runtime, showing how different types of employees can be managed through a common interface while maintaining specific behavior. |
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?
Algorithm :
- Start the program.
- Define an abstract class Account with an abstract method deposit().
- Create a subclass SavingsAccount:
- Override the deposit() method to print “Deposit to Savings Account.”
- Create another subclass CurrentAccount:
- Override the deposit() method to print “Deposit to Current Account.”
- In the Main class:
- Create an Account reference acc1 pointing to a SavingsAccount object and call deposit().
- Create another Account reference acc2 pointing to a CurrentAccount object and call deposit().
- End the program.
Program :
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. |
Explanation :
This program demonstrates abstraction and polymorphism in Java. The abstract class Account defines a method deposit() which is implemented differently in its subclasses SavingsAccount and CurrentAccount. In the main method, Account references are used to refer to these specific account types. When deposit() is called, Java dynamically determines the correct implementation based on the object type, enabling runtime method binding and allowing code flexibility through a common interface. |
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?
Algorithm :
- Start the program.
- Define an abstract class Appliance with an abstract method turnOn().
- Create a subclass WashingMachine:
- Override turnOn() to print “Washing machine is now ON.”
- Create another subclass Refrigerator:
- Override turnOn() to print “Refrigerator is now ON.”
- In the Main class:
- Create an Appliance reference appliance1 pointing to a WashingMachine object and call turnOn().
- Create another Appliance reference appliance2 pointing to a Refrigerator object and call turnOn().
- End the program.
Program :
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. |
Explanation :
This program uses abstraction and runtime polymorphism to model different appliances. The abstract class Appliance defines the general method turnOn(), which is implemented uniquely by WashingMachine and Refrigerator. In the main method, Appliance references are assigned to these specific appliances, and calling turnOn() executes the appropriate version. This design allows treating different appliances uniformly while preserving their distinct behaviors, making the code flexible and maintainable. |
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?
Algorithm :
- Start the program.
- Create an abstract class Animal with an abstract method makeSound().
- Define a class Dog that extends Animal and overrides makeSound() to print “Dog barks.”
- Define another class Cat that extends Animal and overrides makeSound() to print “Cat meows.”
- In the Main class:
- Create an Animal reference animal1 pointing to a Dog object and call makeSound().
- Create another Animal reference animal2 pointing to a Cat object and call makeSound().
- End the program.
Program :
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. |
Explanation :
This program demonstrates abstraction and polymorphism in Java. The abstract class Animal defines a general method makeSound(), which is implemented differently in the Dog and Cat classes. In the main method, Animal references are used to hold objects of different subclasses, and the correct version of makeSound() is executed based on the actual object. This shows how polymorphism allows different animal behaviors to be accessed through a common interface. |
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?
Algorithm :
- Start the program.
- Define an abstract class Shape with an abstract method draw().
- Create a subclass Circle:
- Override the draw() method to print “Drawing a Circle.”
- Create another subclass Square:
- Override the draw() method to print “Drawing a Square.”
- In the Main class:
- Create a Shape reference shape1 and assign it a Circle object.
- Call draw() on shape1.
- Create a Shape reference shape2 and assign it a Square object.
- Call draw() on shape2.
- End the program.
Program :
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. |
Explanation :
This program illustrates abstraction and runtime polymorphism. The abstract class Shape defines a general method draw() that is overridden by its subclasses Circle and Square to provide specific drawing behavior. In the main method, Shape references are used to point to different shape objects, and when draw() is called, the appropriate implementation is executed. This demonstrates how abstract classes allow defining a common structure while enabling specific behavior in subclasses. |
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?
Algorithm :
- Start the program.
- Create an abstract class Vehicle with an abstract method fuelType().
- Define a subclass ElectricCar that extends Vehicle and overrides fuelType() to print “Electric Car uses electricity.”
- Define another subclass GasCar that extends Vehicle and overrides fuelType() to print “Gas Car uses gasoline.”
- In the Main class:
- Create a Vehicle reference vehicle1 and assign it an ElectricCar object.
- Call fuelType() on vehicle1.
- Create another Vehicle reference vehicle2 and assign it a GasCar object.
- Call fuelType() on vehicle2.
- End the program.
Program :
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. |
Explanation :
This program demonstrates the use of abstraction and polymorphism in Java. The abstract class Vehicle defines a method fuelType(), which is implemented differently by its subclasses ElectricCar and GasCar. The main method creates Vehicle references for both types of cars and calls their fuelType() methods. Java dynamically binds the method call to the correct implementation at runtime, allowing different behaviors for different vehicle types while using a common reference type. |
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?
Algorithm :
- Start the program.
- Define an abstract class Person with an abstract method introduce().
- Create a subclass Student that extends Person and overrides introduce() to print “I am a Student.”
- Create another subclass Teacher that extends Person and overrides introduce() to print “I am a Teacher.”
- In the Main class:
- Create a Person reference person1 and assign it a Student object.
- Call introduce() on person1.
- Create another Person reference person2 and assign it a Teacher object.
- Call introduce() on person2.
- End the program.
Program :
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. |
Explanation :
This program demonstrates abstraction and runtime polymorphism. The abstract class Person defines a method introduce() that is implemented differently in its subclasses Student and Teacher. In the main method, Person references are used to refer to both types, and the correct version of introduce() is called at runtime. This approach allows general handling of different types of people while preserving their specific behavior. |
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?
Algorithm :
- Start the program.
- Define an abstract class Building with an abstract method constructionCost().
- Create a subclass House:
- Override constructionCost() to print a fixed cost of $100,000.
- Create another subclass Office:
- Override constructionCost() to print a fixed cost of $500,000.
- In the Main class:
- Create a Building reference building1 pointing to a House object.
- Call constructionCost() on building1.Create another Building reference building2 pointing to an Office object.
- Call constructionCost() on building2.
- End the program.
Program :
abstract class Building { abstract void constructionCost(); } class House extends Building { @Override void constructionCost() { double cost = 100000; // Base cost for house construction 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 |
Explanation :
This program uses abstraction and runtime polymorphism to model construction costs for different building types. The abstract class Building defines the method constructionCost(), which is implemented differently in House and Office. At runtime, when the method is called through a Building reference, the appropriate version based on the actual object (House or Office) is executed. This design makes the code flexible and extendable for future building types. |