What is Example Abstract Class Java?
An abstract class in Java cannot be instantiated and can have abstract methods (without a body) and concrete methods (with a body). Here’s an example:
abstract class Animal {
abstract void makeSound(); // Abstract method
void sleep() { // Concrete method
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
In this example, Animal
is an abstract class with an abstract method makeSound()
and a concrete method sleep()
. The Dog
class extends Animal
and provides an implementation for makeSound()
.