Let me tell you the basic concept first
In the Java language, abstract class and interface are two mechanisms that support abstract class definitions. It is precisely because of the existence of these two mechanisms that Java is given powerful object-oriented capabilities. The abstract class and interface have great similarity in support of abstract class definitions, and can even be replaced by each other. Therefore, many developers seem to have arbitrary choices for abstract class and interface when defining abstract class. In fact, there is still a big difference between the two. The choice of them even reflects whether the understanding of the essence of the problem area and the understanding of the design intention is correct and reasonable.
abstract class and interface are both used in Java language to perform abstract classes (the abstract class in this article is not translated from abstract class, it represents an abstract body, and abstract class is a method used in Java language to define abstract classes. Please pay attention to the distinction). So what is an abstract class? What benefits can use abstract classes bring to us?
In the object-oriented concept, we know that all objects are depicted by classes, but the other way around is not. Not all classes are used to describe objects (concretize classes). If a class does not contain enough information to describe a concrete object, such a class is an abstract class. Abstract classes are often used to represent abstract concepts we obtain in analyzing and designing the problem areas, and are abstractions of a series of concrete concepts that seem different but essentially the same. For example: If we develop a graphic editing software, we will find that there are specific concepts such as circles and triangles in the problem field. They are different, but they all belong to the concept of shape. The concept of shape does not exist in the problem field. It is an abstract concept. It is precisely because abstract concepts do not have corresponding concrete concepts in the problem area that abstract classes used to represent abstract concepts cannot be instantiated.
Virtual functions and interfaces are often used in Java development. What is the difference between these two?
abstract: Only one parent class can be inherited in a subclass
interface: Subclasses can implement multiple interfaces
So when to use abstract and when to use interface?
When a subclass and the parent class are in an is a relationship, for example, an Asian is a person, then an Asian subclass can inherit a person's parent class.
When the subclass and the parent class are like a relationship, for example, a superman in America is like a superman. The superman's abilities are not everyone, so they are not all attributes. We cannot attribute superpowers to human parent class. At this time, we can use interface
Code example:
public abstract class Man {public abstract void eat();} public interface SuperPower {public void superPower();} public class AsiaMan extends Man { @Overridepublic void eat() {System.out.println("Asians eat with chopsticks");}} public class AmericanSuperMan extends Man implements SuperPower {public void superPower() {System.out.println("American Superman can fly");}@Overridepublic void eat() {System.out.println("American eats with knife and fork");}}public static void main(String[] args) {Man anAsiaMan = new AsiaMan();AmericanSuperMan anAmericanSuperMan = new AmericanSuperMan();anAsiaMan.eat();anAmericanSuperMan.eat(); anAmericanSuperMan.superPower();}Running results:
Americans eat with knife and fork American Superman can fly Asians eat with chopsticks
Correct use of abstract and interface is beneficial to improve the readability of the code. When people see extend, they will know what is a. When they see implement, they can know what features this class has like a.