The difference between abstract class and interface and their advantages

Abstract class

What is an abstract class?
If a class does not contain enough information to describe a specific object, such a class is an abstract class.
Characteristics of abstract classes:

  • Abstract classes cannot be instantiated.
  • Abstract classes can contain abstract methods and abstract accessors.
  • The sealed modifier cannot be used to modify the abstract class, because the meaning of the two modifiers is opposite. Classes that use the sealed modifier cannot be inherited, while the abstract modifier requires the class to be inherited.
  • A non-abstract class derived from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.

Why does an abstract class have a constructor? Can't abstract methods be instantiated?
The construction class of the abstract class is not used by itself, but used by the inherited class. The subclass inherits the construction method of the abstract class for initialization.

The benefits of using abstract classes Abstract classes
can be regarded as a format, abstract description of a fixed set of behaviors, but this set of behaviors can have any possible concrete implementation. For example, write the shape as an abstract class, and write the area and length
in the method. In this way, the method of the abstract class must be rewritten when the subclass inherits, so that you can standardize the writing of the subclass, so that others can see their own code and others' code.

interface

What is an interface?
Interface generally refers to an abstraction (which can be another entity) that an entity provides itself to the outside world to separate external communication methods from internal operations so that it can be modified internally without affecting the way other external entities interact with it.
The interface between humans and computers and other information machines or between humans and programs is called user interface. The interface between the hardware components of information machines such as computers is called a hardware interface. The interface between software components of information machines such as computers is called software interface.
In a computer, an interface is a shared boundary for information exchange between two independent components in a computer system. This kind of exchange can occur between computer software and hardware, external devices or people performing operations, or a combination of them.
Benefits of using the interface:

  • Simple and standardized: If a project is relatively large, an architect who can sort out all businesses is needed to define some major interfaces. These interfaces not only tell developers what businesses you need to implement, but also restrict the naming convention (Prevent some developers from naming randomly and making other programmers unable to understand).
  • Maintenance and extensibility: For example, if you want to make a drawing board program, there is a panel class in it, which is mainly responsible for drawing functions, and then you define this class like this.
  • Security and rigor: The interface is an important means to realize loose coupling of software. It describes all external services of the system without involving any specific implementation details. It’s safer and tighter

The methods in the interface are all abstract methods, but the interface cannot have a constructor. However, multiple interfaces can be used, and inheritance can only inherit one.

Guess you like

Origin blog.csdn.net/JL_Java/article/details/109246830