The abstract keyword in Java is used to declare abstract classes and abstract methods. An abstract class cannot be instantiated, and an abstract method must be implemented by subclasses.
Table of Contents
- Introduction
abstractKeyword Syntax- Understanding Abstract Classes and Methods
- Examples
- Abstract Class
- Abstract Method
- Real-World Use Case
- Conclusion
Introduction
The abstract keyword in Java is a crucial part of object-oriented programming. It allows you to create classes and methods that are incomplete and must be implemented by subclasses. This helps in defining a template for future classes.
abstract Keyword Syntax
Abstract Class:
abstract class ClassName {
// fields and methods
}
Abstract Method:
abstract returnType methodName(parameters);
Understanding Abstract Classes and Methods
Abstract Classes:
- Cannot be instantiated.
- Can contain abstract methods (methods without a body) and non-abstract methods (methods with a body).
- Serve as a base class for other classes.
Abstract Methods:
- Declared without an implementation.
- Must be implemented by subclasses.
- Defined only in abstract classes.
Examples
Abstract Class
An abstract class cannot be instantiated and often contains one or more abstract methods.
Example
abstract class Animal {
// Abstract method (does not have a body)
abstract void makeSound();
// Regular method
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
dog.sleep();
}
}
Output:
Bark
Sleeping...
Abstract Method
Abstract methods are declared without implementation and must be implemented by subclasses.
Example
abstract class Shape {
// Abstract method
abstract void draw();
// Non-abstract method
void description() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
circle.draw();
circle.description();
}
}
Output:
Drawing a circle.
This is a shape.
Real-World Use Case
Template Method Pattern
The abstract keyword is often used in the template method design pattern, where a parent class defines the structure of an algorithm, and subclasses implement specific steps.
Example
abstract class Game {
// Template method
final void play() {
start();
playTurn();
end();
}
// Abstract methods to be implemented by subclasses
abstract void start();
abstract void playTurn();
abstract void end();
}
class Chess extends Game {
@Override
void start() {
System.out.println("Starting Chess Game");
}
@Override
void playTurn() {
System.out.println("Playing Chess Turn");
}
@Override
void end() {
System.out.println("Ending Chess Game");
}
}
public class Main {
public static void main(String[] args) {
Game game = new Chess();
game.play();
}
}
Output:
Starting Chess Game
Playing Chess Turn
Ending Chess Game
Conclusion
The abstract keyword in Java is used for defining incomplete classes and methods that serve as a blueprint for other classes. Abstract classes and methods provide a way to enforce a contract for subclasses, ensuring that they implement specific behaviors. By understanding and using the abstract keyword, you can create flexible and extensible code that follows the principles of object-oriented programming.