The abstract
Modifier in Java
The abstract
modifier in Java is used for classes and methods to indicate that they are incomplete and must be implemented by subclasses.
1. Where Can We Use abstract
?
Where? | Purpose |
---|---|
Abstract Class | Prevents direct instantiation and provides a base for subclasses. |
Abstract Method | Declares a method without an implementation, to be implemented by subclasses. |
2. Abstract Classes (abstract class
)
An abstract class:
- Cannot be instantiated (objects cannot be created directly).
- Can contain abstract methods (methods without a body).
- Can have both abstract and concrete (implemented) methods.
Example: Abstract Class
abstract class Animal {
abstract void makeSound(); // Abstract method (must be implemented in subclasses)
void sleep() { // Concrete method (has implementation)
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
void makeSound() { // Implementing the abstract method
System.out.println("Bark!");
}
}
public class AbstractExample {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Output: Bark!
myDog.sleep(); // Output: Sleeping...
}
}
✔ Key Points:
- The
Animal
class is abstract because it has an abstract method (makeSound()
). - Subclasses (
Dog
) must implement the abstract method. - Concrete methods (
sleep()
) are inherited by subclasses.
3. Abstract Methods (abstract method
)
- An abstract method has no body (
{}
is missing). - Must be implemented by subclasses.
- Can only exist inside an abstract class.
Example: Abstract Method
abstract class Shape {
abstract void draw(); // Abstract method (no implementation)
}
class Circle extends Shape {
void draw() { // Implementation of abstract method
System.out.println("Drawing a Circle");
}
}
✔ The draw()
method must be implemented in Circle
.
4. Why Use abstract
?
Feature | Reason |
---|---|
Encapsulation of Common Behavior | Abstract classes allow defining common methods while forcing subclasses to implement specific behaviors. |
Prevents Object Creation | You cannot create an instance of an abstract class. |
Forces Implementation | Ensures that subclasses implement necessary methods. |
5. Can an Abstract Class Have a Constructor?
Yes! An abstract class can have a constructor, but it cannot be instantiated directly.
Example
abstract class Vehicle {
Vehicle() {
System.out.println("Vehicle created");
}
}
class Car extends Vehicle {
Car() {
System.out.println("Car created");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
}
}
✔ Output:
Vehicle created
Car created
✔ Why use a constructor?
- It allows initialization of common fields.
- Subclasses can call
super()
to use it.
6. Abstract Class vs. Interface
Feature | Abstract Class | Interface |
---|---|---|
Methods | Can have abstract and concrete methods | Only abstract methods (before Java 8), can have default/static methods (Java 8+) |
Variables | Can have instance variables | Only public static final (constants) |
Constructors | ✅ Can have constructors | ❌ No constructors |
Inheritance | A class can extend only one abstract class | A class can implement multiple interfaces |