In this lesson, you will learn
Sometimes, there are situations in which you want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.
In that case, you will define that class as an Abstract class.
Note: An abstract class is exactly opposite to the final modifier. The final method can’t be overridden in their subclasses, but their subclasses must override the abstract method.

abstract keyword before the class keyword.Here is the syntax to define the abstract method.
abstract returntype method-name(parameter-list);
As you can see, no method body is present.
Any class that contains one or more abstract methods must also be declared as an abstract class..
Here’s the syntax to demonstrate abstract class in Java.
public abstract class ClassName {
// Field declarations
int field1;
String field2;
// Constructor
public ClassName(int field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
// Abstract method (does not have a body)
public abstract void abstractMethod();
// Concrete method (has a body)
public void concreteMethod() {
// Implementation code
System.out.println("This is a concrete method.");
}
}
Here are the features of an abstract class in Java, explained in bullet points:
new keyword.abstract keyword and have no implementation (no method body).extends keyword.final.static.
package ch10;
//A Simple demonstration of abstract.
abstract class First {
//abstract method without body
abstract void callingme();
// concrete methods with body
void callingmetoo() {
System.out.println("It is a concrete method.");
}
}
class Second extends First {
void callingme() {
System.out.println("Subclass implementing callingme() of superclass");
}
}
public class AbstractDemo {
public static void main(String[] args) {
Second objSecond=new Second();
objSecond.callingme();
objSecond.callingmetoo();
}
}
Subclass implementing callingme() of superclass
It is a concrete method.

package ch10;
abstract class Animal {
// Field for the name of the animal
private String name;
// Constructor to set the name of the animal
public Animal(String name) {
this.name = name;
}
// Abstract method makeSound
public abstract void makeSound();
// Concrete method to get the name of the animal
public String getName() {
return name;
}
public void eat() {
System.out.println(getName()+" is eating food");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name); // Call the constructor of the Animal class
}
// Implementing the abstract method for Dog
@Override
public void makeSound() {
System.out.println(getName() + " says: Bark");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name); // Call the constructor of the Animal class
}
// Implementing the abstract method for Cat
@Override
public void makeSound() {
System.out.println(getName() + " says: Meow");
}
}
public class AceesAnimal {
public static void main(String[] args) {
Animal dog = new Dog("Buddy");
dog.makeSound(); // Output: Buddy says: Bark
dog.eat(); //Output: Buddy is eating food
Animal cat = new Cat("Whiskers");
cat.makeSound(); // Output: Whiskers says: Meow
cat.eat(); //Output: Buddy is eating food
}
}
Buddy says: Bark
Buddy is eating food
Whiskers says: Meow
Whiskers is eating food
Animal: This class provides a template for animal objects with a common field name and a constructor to set this name. It declares an abstract method makeSound(), which forces subclasses to provide their own implementation of how the animal makes a sound.Dog, Cat): These classes extend the Animal class and provide specific implementations of the makeSound() method. For example, Dog prints out “Bark” and Cat prints out “Meow”.Main class, we instantiate Dog and Cat objects as Animal types and call the makeSound() method on them. This demonstrates polymorphism; the correct makeSound() method is called at runtime based on the actual object type.
abstract class Shape {
abstract double area();
abstract double perimeter();
void displayShapeType() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
@Override
double perimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends Shape {
double width;
double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
double area() {
return width * height;
}
@Override
double perimeter() {
return 2 * (width + height);
}
}
public class ShapeDemo {
public static void main(String[] args) {
// Shape shape = new Shape(); // Error: Cannot instantiate abstract class
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Circle Area: " + circle.area());
System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
circle.displayShapeType();
rectangle.displayShapeType();
}
}
Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Area for Figure is undefined.
Area is 0.0
Q: What is an abstract class in Java?
A: An abstract class is a class that cannot be instantiated. It serves as a blueprint for other classes, defining common properties and behaviors that subclasses must inherit. It can contain both abstract and concrete (non-abstract) methods.
Q: What is an abstract method in Java?
A: An abstract method is a method declared with the abstract keyword that has no implementation (no body). It only has a method signature. Abstract methods must be implemented by concrete (non-abstract) subclasses.
Q: How do I declare an abstract class?
A: Use the abstract keyword before the class keyword in the class declaration:
abstract class MyAbstractClass {
// ...
}
Q: How do I declare an abstract method?
A: Use the abstract keyword before the return type of the method and end the method declaration with a semicolon:
abstract void myAbstractMethod();
Q: Can I create an object of an abstract class?
A: No, you cannot create an instance of an abstract class directly using the new keyword. Abstract classes are meant to be subclassed.
Q: Can an abstract class have a constructor?
A: Yes, abstract classes can have constructors. The constructor is called when a subclass object is created.
Q: Can an abstract class have non-abstract (concrete) methods?
A: Yes, abstract classes can contain both abstract and non-abstract methods. Non-abstract methods provide default implementations that subclasses can inherit.
Q: What happens if a subclass does not implement an abstract method?
A: If a subclass does not implement all abstract methods of its superclass, the subclass must also be declared as abstract.
Q: Can a non-abstract class extend an abstract class?
A: Yes, a non-abstract (concrete) class can extend an abstract class. However, the concrete subclass must provide implementations for all abstract methods of the superclass.
Q: What is the purpose of using abstract classes and abstract methods?
A: They promote abstraction and code reusability. They allow you to define a common interface and behavior for a group of related classes, while deferring the implementation details to subclasses.
Q: Can an abstract class implement interfaces?
A: Yes, abstract classes can implement interfaces. They can provide default implementations for some or all of the interface methods.
Q: Can an abstract method be final?
A: No, abstract methods cannot be final. final methods cannot be overridden, but abstract methods must be overridden by the subclasses.
Q: Can an abstract method be static?
A: No, abstract methods cannot be static. static methods belong to the class itself, while abstract methods are meant to be implemented by subclasses.
Q: When should I use an abstract class vs. an interface?
A:
Car is a Vehicle).Printer can print). Interfaces are also useful for multiple inheritance of type.Q: Can an abstract class have instance variables (fields)?
A: Yes, abstract classes can have instance variables. These variables can be inherited by subclasses.
Q: How does the super() keyword related to abstract classes?
A: The super() keyword is used in a subclass constructor to call the constructor of its superclass (which can be an abstract class). This ensures that the superclass’s constructor is executed, initializing the superclass’s state.
—————————————————————
Objective: Create a Java program that models a simple vehicle hierarchy using abstract classes and inheritance. The hierarchy should differentiate vehicles by their method of movement.
Requirements:
Vehicle with:
name and speed.move() that describes how the vehicle moves.displayInfo() to print the vehicle’s name and speed.Car, Boat, and Airplane, inheriting from Vehicle:
Car should override the move() method to indicate it moves by driving on roads.Boat should override the move() method to indicate it moves by sailing on water.Airplane should override the move() method to indicate it moves by flying in the air.main method, instantiate each of the three vehicle types, and call their move() and displayInfo() methods.
Objective: Implement a system to manage different types of members in a university using abstract classes and inheritance.
Requirements:
UniversityMember with:
name, age, and department.getRole() that returns the member’s role in the university (e.g., “Student”, “Faculty”).printDetails() to output the member’s information.Student and Faculty, inheriting from UniversityMember:
Student should have additional fields for studentID and major, and implement the getRole() method.Faculty should have additional fields for facultyID and specialization, and implement the getRole() method.main method, create a list of UniversityMember objects that includes both Student and Faculty instances. Iterate over the list, calling printDetails() for each member.
Objective: Design a simple Java program to represent a credit card system using abstract classes and inheritance, focusing on different types of credit cards.
Requirements:
CreditCard:
cardNumber, cardHolderName, and balance.calculateInterest() that calculates the interest based on the balance and returns it.displayCardInfo() to print the card number, holder’s name, and balance.RewardsCard, CashBackCard, and TravelCard inheriting from CreditCard:
calculateInterest() method. The method should calculate interest differently based on the type of card:
RewardsCard might have a lower interest rate.CashBackCard might offer cash back on the interest calculated.TravelCard might have benefits related to travel expenses.RewardsCard might have points).main method:
displayCardInfo() method and calculateInterest() on each object to show how each card calculates interest differently.
Objective: Build a Java program to model an automobile hierarchy using abstract classes and inheritance, emphasizing the different types of automobiles and their functionalities.
Requirements:
Automobile:
make (the manufacturer), model, and year.fuelType() which returns the type of fuel the automobile uses (e.g., gasoline, diesel, electric).displayDetails() to print the make, model, and year of the automobile.Sedan, SUV, and ElectricCar inheriting from Automobile:
Sedan and SUV should implement the fuelType() method to return “gasoline” or “diesel”, respectively.ElectricCar should implement the fuelType() method to return “electric”.SUV might have a field for off-road capability).main method:
displayDetails() and fuelType() methods on each object to demonstrate polymorphism.
clear and understandable
You must be logged in to submit a review.