abstract Keyword in Java
The abstract
keyword in Java is used to declare a class or a method that cannot be instantiated directly or must be implemented by subclasses, respectively. It is a key part of Java's abstraction mechanism, allowing developers to define abstract classes and methods that provide a blueprint for other classes.
Usage
Abstract Class
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods that must be implemented by subclasses.
Abstract Method
An abstract method is a method that is declared without an implementation. Subclasses of the abstract class are required to provide an implementation for these methods.
Syntax
Abstract Class
abstract class ClassName {
// Abstract methods
abstract void methodName();
// Concrete methods
void concreteMethod() {
// Method body
}
}
Abstract Method
abstract void methodName();
Examples
Example 1: Abstract Class and Method
abstract class Animal {
// Abstract method
abstract void makeSound();
// Concrete method
void sleep() {
System.out.println("Sleeping...");
}
}
class Dog extends Animal {
// Implementing the abstract method
void makeSound() {
System.out.println("Bark");
}
}
public class AbstractExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Bark
dog.sleep(); // Outputs: Sleeping...
}
}
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 the makeSound()
method.
Example 2: Multiple Abstract Methods
abstract class Shape {
abstract void draw();
abstract double area();
}
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
void draw() {
System.out.println("Drawing Circle");
}
double area() {
return Math.PI * radius * radius;
}
}
public class ShapeExample {
public static void main(String[] args) {
Circle circle = new Circle(5);
circle.draw(); // Outputs: Drawing Circle
System.out.println("Area: " + circle.area()); // Outputs: Area: 78.53981633974483
}
}
Here, Shape
is an abstract class with two abstract methods: draw()
and area()
. Circle
extends Shape
and provides implementations for both abstract methods.
Tips and Best Practices
- Use Abstract Classes for Shared Code: Use abstract classes when you have shared code that should be inherited by multiple subclasses. Abstract classes can contain both abstract and concrete methods.
- Mandatory Implementation: Ensure that all abstract methods in an abstract class are implemented by its subclasses.
- Cannot Instantiate: Remember that you cannot create instances of an abstract class directly. You must create an instance of a subclass that implements all abstract methods.
- Use Abstract Methods for Interfaces: If you have a class that will serve as a base with methods that must be overridden, use abstract methods to enforce this contract.
- Combining with Interfaces: Abstract classes can implement interfaces and provide implementations for some of the methods, while leaving others as abstract for subclasses to implement.
abstract class Vehicle implements Movable { abstract void start(); }
- Constructor in Abstract Class: Abstract classes can have constructors, which can be called during the instantiation of a subclass.
abstract class Base { Base() { System.out.println("Base Constructor"); } }
- Abstract Classes vs Interfaces: Use abstract classes when you need to share code among closely related classes, and use interfaces when you need to define a contract that can be implemented by any class, regardless of its position in the class hierarchy.
- Static Methods and Fields: Abstract classes can have static fields and methods, which can be accessed without creating an instance of the class.
abstract class Utility {
static void printMessage() {
System.out.println("Utility Message");
}
}