$ sudo teach IT
МОДУЛЬ 8 · УРОК 3

Abstract Classes

Abstract classes cannot be instantiated. They serve as bases for other classes.

30 minBeginnerJavaOOP

abstract class

abstract class Animal {
    abstract void sound(); // no body
    void eat() { }         // has body
}

Cannot create Animal with new. Must subclass.

Abstract Methods

Abstract methods have no body. Concrete subclasses MUST implement all abstract methods.

abstract class Shape {
    abstract double area();
    abstract double perimeter();
}
class Circle extends Shape {
    double area() { return 3.14 * r * r; }
    double perimeter() { return 2 * 3.14 * r; }
}

Template Method

abstract class Report {
    void generate() { header(); body(); footer(); }
    abstract void header();
    abstract void body();
    abstract void footer();
}

✅ Итоги урока

  • Abstract class: cannot be instantiated (abstract keyword).
  • Abstract methods: no body, subclasses must override.
  • Concrete subclass must implement ALL abstract methods.
  • Abstract classes can have constructors (called via super()).
  • Template Method pattern: abstract class defines algorithm skeleton.

Next lesson: interfaces.

Test: Test: Abstract Classes

3 вопроса

Абстрактный класс Shape

Premium