What is the difference between an interface and abstract class?

前端 未结 30 1862
情歌与酒
情歌与酒 2020-11-21 11:51

What exactly is the difference between an interface and abstract class?

30条回答
  •  一个人的身影
    2020-11-21 12:19

    You can find clear difference between interface and abstract class.

    Interface

    • Interface only contains abstract methods.
    • Force users to implement all methods when implements the interface.
    • Contains only final and static variables.
    • Declare using interface keyword.
    • All methods of an interface must be defined as public.
    • An interface can extend or a class can implement multiple other interfaces.

    Abstract class

    • Abstract class contains abstract and non-abstract methods.

    • Does not force users to implement all methods when inherited the abstract class.

    • Contains all kinds of variables including primitive and non-primitive

    • Declare using abstract keyword.

    • Methods and members of an abstract class can be defined with any visibility.

    • A child class can only extend a single class (abstract or concrete).

提交回复
热议问题