- The
abstractkeyword in Java is used to declare a class or method that is incomplete and must be implemented in subclasses. - It plays a crucial role in object-oriented programming by enabling ==abstraction, which allows you to define abstract classes and methods that set a blueprint for derived classes.
1. Definition
- Abstract Class: A class that cannot be instantiated on its own and is intended to be subclassed.
- It may contain abstract methods and concrete methods (methods with an implementation).
- Abstract Method: A method that is declared without an implementation. Subclasses are required to provide an implementation for this method.
2. Characteristics of Abstract Classes
- Cannot be Instantiated: You cannot create an instance of an abstract class directly.
- Can Contain Concrete Methods: An abstract class can have fully defined methods (with a body) alongside abstract methods.
- Can Have Constructors: Abstract classes can have constructors, which can be called when a subclass is instantiated.
- Supports Multiple Abstract Methods: An abstract class can have multiple abstract methods, which must be implemented by its subclasses.
3. Syntax
- Abstract Class Declaration:
abstract class ClassName {
// Abstract method
abstract void abstractMethod();
// Concrete method
void concreteMethod() {
// Method implementation
}
}- Abstract Method Declaration:
abstract returnType methodName(parameters);4. Example of Abstract Class and Method
// Abstract class
abstract class Animal {
// Abstract method
abstract void makeSound();
// Concrete method
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass that extends the abstract class
class Dog extends Animal {
@Override
void makeSound() { // Implementing the abstract method
System.out.println("Woof");
}
}
class Cat extends Animal {
@Override
void makeSound() { // Implementing the abstract method
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog(); // Instantiate subclass
dog.makeSound(); // Outputs: Woof
dog.eat(); // Outputs: This animal eats food.
Animal cat = new Cat(); // Instantiate another subclass
cat.makeSound(); // Outputs: Meow
cat.eat(); // Outputs: This animal eats food.
}
}5. Use Cases
- Defining Common Behavior: Abstract classes allow you to define common behavior for all subclasses, enforcing a contract for derived classes to implement specific methods.
- Creating Template Methods: Use abstract methods to create a template for operations while allowing subclasses to define specific implementations.
6. Abstract Classes vs. Interfaces
-
Abstract Class: Can have both abstract and concrete methods, can have state (instance variables), and can have constructors. A class can extend only one abstract class. no multiple inheritance
-
Interface: Can only have abstract methods (Java 8 and later versions allow default methods), cannot have instance variables, and a class can implement multiple interfaces. capable of multiple inheritance