The abstract keyword in Java is used to achieve abstraction by hiding implementation details and showing only essential functionality. It can be used with both classes and methods.
- Used to achieve abstraction in Java
- Abstract classes can contain both abstract and non-abstract methods
- Abstract methods do not have a method body
- Child classes must implement abstract methods
Note: An abstract class cannot be instantiated directly and is mainly used as a base class for inheritance.
Real-World Example
Consider a vehicle system:
- Every vehicle can start
- But the starting process differs for Car, Bike, and Bus
Instead of defining the implementation in a common class, we declare a common abstract method and allow child classes to provide their own implementation.
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car Starts With Key");
}
}
public class Geeks{
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}
Output
Car Starts With Key
Declaration of Abstract Class
An abstract class is declared using the abstract keyword.
abstract class ClassName {
}
Declaration of Abstract Method
An abstract method contains only method declaration and no implementation.
abstract returnType methodName();
abstract class Employee {
abstract void salary();
void company() {
System.out.println("GeeksforGeeks");
}
}
class Developer extends Employee {
void salary() {
System.out.println("Salary is 80,000");
}
}
public class Main {
public static void main(String[] args) {
Developer d = new Developer();
d.salary();
d.company();
}
}
Output
Salary is 80,000 GeeksforGeeks
Important Rules of Abstract Keyword
The abstract keyword follows several important rules that define how abstract classes and methods behave in Java.
1. Abstract Class Cannot Be Instantiated
An abstract class cannot be used to create objects directly.
abstract class Animal {
}
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
}
}
Output:
Animal is abstract; cannot be instantiated
2. Abstract Method Must Be Overridden
If a class extends an abstract class, it must implement all abstract methods.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
3. Abstract Class Can Have Normal Methods
An abstract class may contain both:
- abstract methods
- concrete methods
abstract class Animal {
abstract void sound();
void sleep() {
System.out.println("Animal is Sleeping");
}
}
4. Abstract Class Can Have Constructors
Abstract classes can contain constructors.
abstract class Vehicle {
Vehicle() {
System.out.println("Vehicle Constructor Called");
}
}
Abstract Class vs Concrete Class
| Abstract Class | Concrete Class |
|---|---|
| Cannot create objects | Objects can be created |
| May contain abstract methods | Contains fully implemented methods |
| Used for abstraction | Used for normal object creation |
| Must be inherited | Can be used directly |