Understanding the purpose of Abstract Classes in Java

前端 未结 8 1720
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 06:15

Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same

8条回答
  •  旧时难觅i
    2020-12-01 06:33

    The main difference between abstract classes and interfaces, is that interfaces only define an interface and abstract classes can also have an implementation and fields/properties.

    An abstract class can be seen as a IS-A relation (e.g. a horse is an animal). For interface it mostly is some feature a class can have (e.g. INavigable, ISortable).

    Also abstract methods cannot be instantiated (neither to interfaces).

    Overriding functions are to show that a function has a base class implementation.

    Also multiple inheritance is not possible/adviced with classes, therefore max. use only one base class and inherit from interfaces for other features.

提交回复
热议问题