0
0
JavaHow-ToBeginner · 3 min read

How to Create Abstract Class in Java: Syntax and Examples

In Java, you create an abstract class by using the abstract keyword before the class keyword. An abstract class can have abstract methods (without body) and concrete methods (with body), and it cannot be instantiated directly.
📐

Syntax

An abstract class is declared using the abstract keyword before the class name. It can contain abstract methods (methods without a body) that must be implemented by subclasses, and regular methods with implementations.

  • abstract class ClassName: Declares the class as abstract.
  • abstract returnType methodName();: Declares an abstract method without a body.
  • Concrete methods: Methods with full implementation.
java
abstract class Animal {
    abstract void sound(); // abstract method
    void sleep() {
        System.out.println("Sleeping...");
    }
}
💻

Example

This example shows an abstract class Animal with an abstract method sound() and a concrete method sleep(). The subclass Dog implements the abstract method. The program creates a Dog object and calls both methods.

java
abstract class Animal {
    abstract void sound();
    void sleep() {
        System.out.println("Sleeping...");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound();
        dog.sleep();
    }
}
Output
Bark Sleeping...
⚠️

Common Pitfalls

  • Trying to create an object of an abstract class directly causes a compile-time error.
  • Not implementing all abstract methods in a subclass results in a compile-time error unless the subclass is also abstract.
  • Abstract methods cannot have a body.

Example of wrong and right usage:

java
abstract class Vehicle {
    abstract void move();
}

// Wrong: Cannot instantiate abstract class
// Vehicle v = new Vehicle(); // Error

class Car extends Vehicle {
    @Override
    void move() {
        System.out.println("Car is moving");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car(); // Correct
        v.move();
    }
}
Output
Car is moving
📊

Quick Reference

  • Use abstract keyword before class to declare an abstract class.
  • Abstract classes cannot be instantiated directly.
  • Subclasses must implement all abstract methods or be declared abstract themselves.
  • Abstract methods have no body and end with a semicolon.
  • Abstract classes can have constructors, fields, and concrete methods.

Key Takeaways

Use the abstract keyword before class to create an abstract class in Java.
Abstract classes cannot be instantiated directly; you must create objects of subclasses.
All abstract methods must be implemented by subclasses unless the subclass is also abstract.
Abstract methods have no body and end with a semicolon.
Abstract classes can contain both abstract and concrete methods.