Thursday, January 16, 2025
HomeProgrammingWhat is Abstract Class in Java

What is Abstract Class in Java

An abstract class in Java is a class that cannot be instantiated and is used as a blueprint for other classes. It can include both abstract methods (without implementation) and concrete methods (with implementation).

Key Features:

1. Abstract Methods: Declared without a body; subclasses must override them.

See also  How to Check RAM in Linux

2. Concrete Methods: Can be defined within the abstract class and inherited by subclasses.

3. Inheritance: Serves as a base class for subclasses, promoting code reusability and design flexibility.

Example:

abstract class Animal {
abstract void sound();

See also  Architecture - What's the Difference between REST & RESTful?

void eat() {
System.out.println(“This animal eats food”);
}
}

class Dog extends Animal {
void sound() {
System.out.println(“Bark”);
}
}

In this example, Animal is an abstract class with an abstract method sound() and a concrete method eat(). The Dog class extends Animal and provides an implementation for the sound() method.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x