Detail the difference between abstract abstract classes and interfaces

Detail the difference between abstract abstract classes and interfaces

Abstract class

Abstract class: The class decorated with abstract is an abstract class
Abstract method: The method decorated with abstract is an abstract method, and the method is only declared without the method body
characteristics:

  • Abstract classes cannot be instantiated, that is, you cannot create objects with the keyword new
  • A subclass of an abstract class must implement all abstract methods of that class, otherwise the subclass is also an abstract class
  • Abstract methods in abstract classes are a form of polymorphism
  • There can be specific methods, which can be called with subclasses

Note : abstract cannot be used with final, private, static

interface

Interface: is a collection of abstract methods, decorated with interface
features:

  • The interface only includes constants (default public static final decoration), abstract methods (must be public) and inner classes
  • Can't create objects and have no construction method
  • Interfaces can only inherit interfaces, and can inherit multiple interfaces.
  • A class implements an interface, but does not implement all abstract methods in the interface, then this class is an abstract class
  • The same abstract method is defined in multiple interfaces, and only one of them needs to be implemented
  • Beginning with Java 8, one or more non-abstract methods decorated by static can be added to the interface, which are directly called by the interface or implementation class (static method implementation)
The difference between abstract classes and interfaces
the difference Abstract class interface
Keyword abstact interface
Construction method Have No
inherit Single inheritance Multiple inheritance
Implementation extends implements
Member variables Any legal member variable Only public static finnal variables
method Any legal method Only abstract methods, JDK8 can have specific methods modified by static or default
Published 4 original articles · Likes0 · Visits 27

Guess you like

Origin blog.csdn.net/qq_45738402/article/details/105147942