Java.Where and for what is the abstract modifier used?

The abstract Modifier in Java

The abstract modifier in Java is used for classes and methods to indicate that they are incomplete and must be implemented by subclasses.


1. Where Can We Use abstract?

Where?Purpose
Abstract ClassPrevents direct instantiation and provides a base for subclasses.
Abstract MethodDeclares a method without an implementation, to be implemented by subclasses.

2. Abstract Classes (abstract class)

An abstract class:

  • Cannot be instantiated (objects cannot be created directly).
  • Can contain abstract methods (methods without a body).
  • Can have both abstract and concrete (implemented) methods.

Example: Abstract Class

abstract class Animal {
    abstract void makeSound(); // Abstract method (must be implemented in subclasses)

    void sleep() { // Concrete method (has implementation)
        System.out.println("Sleeping...");
    }
}

class Dog extends Animal {
    void makeSound() { // Implementing the abstract method
        System.out.println("Bark!");
    }
}

public class AbstractExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound(); // Output: Bark!
        myDog.sleep(); // Output: Sleeping...
    }
}

Key Points:

  • The Animal class is abstract because it has an abstract method (makeSound()).
  • Subclasses (Dog) must implement the abstract method.
  • Concrete methods (sleep()) are inherited by subclasses.

3. Abstract Methods (abstract method)

  • An abstract method has no body ({} is missing).
  • Must be implemented by subclasses.
  • Can only exist inside an abstract class.

Example: Abstract Method

abstract class Shape {
    abstract void draw(); // Abstract method (no implementation)
}

class Circle extends Shape {
    void draw() { // Implementation of abstract method
        System.out.println("Drawing a Circle");
    }
}

✔ The draw() method must be implemented in Circle.


4. Why Use abstract?

FeatureReason
Encapsulation of Common BehaviorAbstract classes allow defining common methods while forcing subclasses to implement specific behaviors.
Prevents Object CreationYou cannot create an instance of an abstract class.
Forces ImplementationEnsures that subclasses implement necessary methods.

5. Can an Abstract Class Have a Constructor?

Yes! An abstract class can have a constructor, but it cannot be instantiated directly.

Example

abstract class Vehicle {
    Vehicle() {
        System.out.println("Vehicle created");
    }
}

class Car extends Vehicle {
    Car() {
        System.out.println("Car created");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
    }
}

✔ Output:

Vehicle created
Car created

Why use a constructor?

  • It allows initialization of common fields.
  • Subclasses can call super() to use it.

6. Abstract Class vs. Interface

FeatureAbstract ClassInterface
MethodsCan have abstract and concrete methodsOnly abstract methods (before Java 8), can have default/static methods (Java 8+)
VariablesCan have instance variablesOnly public static final (constants)
Constructors✅ Can have constructors❌ No constructors
InheritanceA class can extend only one abstract classA class can implement multiple interfaces
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.