In the last article, I talked about interfaces. https://qiita.com/QiitaD/items/835ed6ee4e52cb6b214a This time, I will describe "abstract classes" that are often compared with interfaces.
A class that has one or more abstract methods. Abstract methods must be implemented in subclasses, so you can force developers to override them.
There are the following rules for handling abstract classes.
The following is a table of the differences.
Abstract class | interface | |
---|---|---|
Method with implementation | Can be implemented | Cannot be implemented |
Member variables | Class member variables | constant |
Multiple inheritance | Impossible | Yes |
Can be implemented as follows
//抽象クラスViecleの実装
abstract class Viecle {
//抽象メソッド
}
Subclasses can be implemented by inheriting abstract classes.
//抽象クラスViecleの実装
abstract class Car extends Vielce {
//スーパークラスのコンストラクタ呼び出し
//抽象メソッドのオーバーライド
}
Recommended Posts