What is an abstract modifier?

The abstract modifier is used to declare something that is incomplete and meant to be extended or implemented by other classes. You can apply it to both classes and methods, and here’s what it means in each case.

🔹 Abstract Class

An abstract class is a class that cannot be instantiated on its own—it’s like a blueprint for other classes.

You can’t do new Animal(), but you can create subclasses that extend it and provide implementations for its abstract methods.

🔹 Abstract Method

An abstract method is a method without a body, declared in an abstract class. Subclasses are required to provide an implementation.

So, abstract is Java’s way of saying, “Here’s the framework—now you fill in the details.”

Example :

// Abstract class
abstract class Animal {
// Abstract method (no body)
abstract void sound();

// Regular method
void breathe() {
    System.out.println("All animals breathe.");
}

}


// Subclass of Animal
class Dog extends Animal {
// Implementing the abstract method
void sound() {
System.out.println(“Dog says: Woof!”);
}
}

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // Create a Dog object
myDog.sound(); // Calls the implemented method
myDog.breathe(); // Calls the regular method
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *