Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Sự khác biệt giữa Abstract Class và Concrete Class trong Java

Abstract Class là class không thể khởi tạo trực tiếp, có thể chứa method abstract (chưa có body) và method concrete (có body). Concrete Class là class thông thường, có thể khởi tạo, tất cả method đều có body.

Bảng so sánh

STTTiêu chíAbstract ClassConcrete Class
1 Method Có thể có abstract method (không body) VÀ concrete method (có body). Chỉ có concrete method. Một abstract method cũng làm class thành abstract.
2 Khởi tạo (Instantiation) Không thể new AbstractClass(). Chỉ dùng làm base class. Có thể new ConcreteClass().
3 Abstract method Có thể có 0 hoặc nhiều. Không bắt buộc phải có. Không được có abstract method.
4 Final Không thể khai báo final (vì cần extends). Có thể khai báo final (không cho extends).
5 Từ khóa Bắt buộc abstract class. Không có abstract.
6 Kế thừa extends class khác, implements interface. extends class (abstract/concrete), implements interface.
7 Interface Có thể implement interface. Subclass concrete mới instantiate được. Implement interface trực tiếp.

Ví dụ

public class JavaTester {
    public static void main(String args[]) {
        // Cat lion = new Cat(); // COMPILE ERROR: Cannot instantiate abstract class
        Cat lion = new Lion(); // Polymorphism: Abstract reference, Concrete object
        lion.eat();
    }
}

// Abstract class: Template
abstract class Cat {
    // Abstract method - bắt buộc subclass implement
    abstract public void eat();
    
    // Concrete method - chia sẻ code chung
    public void breathe() {
        System.out.println("Breathing...");
    }
}

// Concrete class: Implementation hoàn chỉnh
class Lion extends Cat {
    @Override
    public void eat() {
        System.out.println("Lion eats meat");
    }
}

// Concrete class khác
class Tiger extends Cat {
    @Override
    public void eat() {
        System.out.println("Tiger eats meat");
    }
    
    // Có thể thêm method riêng
    public void roar() {
        System.out.println("Roar!");
    }
}

Kết quả

Lion eats meat

Khi nào dùng Abstract Class?

  • Chia sẻ code chung (common implementation) cho các subclass liên quan.
  • Cần định nghĩa template method (algorithm skeleton) - subclass override bước cụ thể.
  • Cần instance field (state) chung cho các subclass.
  • Cần constructor để khởi tạo state chung.
  • Hierarchy đơn (single inheritance) - "is-a" relationship chặt chẽ.

Khi nào dùng Concrete Class?

  • Implementation hoàn chỉnh, sẵn sàng dùng.
  • Không cần subclass (hoặc dùng final).
  • Leaf class trong hierarchy.

Abstract Class vs Interface (Java 8+)

Đặc điểmAbstract ClassInterface
Instance fieldCóKhông
ConstructorCóKhông
Multiple inheritKhôngCó
Default methodCó (instance method)Có (Java 8+)
Private methodCóCó (Java 9+)
Access modifierTất cảMặc định public

Quy tắc chọn (Effective Java)

  • Ưu tiên Interface để định nghĩa type/contract (API public, SPI, multiple impl).
  • Dùng Abstract Class khi cần chia sẻ implementation + state, hoặc skeleton algorithm (Template Method pattern).
  • Có thể kết hợp: Interface public + Abstract class skeletal implementation (ví dụ: AbstractList, AbstractMap).