Difference between interface and abstract class

A class that declares the existence of a method without implementing it is called an abstract class. It is used to create a class that embodies some basic behavior, and declares methods for the class, but cannot implement the class in the class. Case.
An instance of the abstract class cannot be created.
It is however possible to create a variable whose type is an abstract class and have it point to an instance of a concrete subclass.
Cannot have abstract constructors or abstract static methods. Subclasses of the Abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes.
Instead, implement the method in a subclass. Other classes that know their behavior can implement these methods in the class.
An interface is a variant of an abstract class.
In an interface, all methods are abstract. Multiple inheritance can be achieved by implementing such an interface.
All the methods in the interface are abstract, none of them have a program body. An interface can only define static final member variables.
An implementation of an interface is similar to a subclass, except that the implementing class cannot inherit behavior from the interface definition.
When a class implements a special interface, it defines (ie gives the program body) all the methods of that interface.
It can then call the interface's methods on any object of the class that implements the interface.
Since there are abstract classes, it allows to use the interface name as the type of the reference variable. The usual dynamic binding will take effect.
References can be converted to and from interface types, and the instanceof operator can be used to determine whether an object's class implements the interface.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326967312&siteId=291194637