Working for many years, you may not really understand interfaces and abstract classes

Time:2021-3-13

Working for many years, you may not really understand interfaces and abstract classes

  1. What are the definitions and differences between abstract classes and interfaces?
  2. What problems do abstract classes solve in object oriented programming?
  3. What problems does interface solve in object oriented programming?
  4. How to decide whether to use abstract class or interface?
  5. If the language used does not support abstract classes and interfaces, how to deal with it?

abstract class

In object-oriented programming languages, most of them add support for abstract classes and interfaces, such as Java, C #, etc.

//Abstract class
    public abstract class Human
    {
        //Abstract method
        public abstract string Gender();
        //Properties
        public string Name { get; set; }
        //Methods
        public int GetAge()
        {
            return 1;
        }
    }

The above is the definition of a common abstract class. There are a lot of results about how to use it. In fact, in general, the abstract class mainly has the following characteristics:

  1. Abstract classes cannot be instantiated, they can only be inherited. That is to say, new human() will report a compilation error
  2. Abstract class is also a class, which can contain properties and methods, and methods can contain or not contain implementation, which is called abstract method.
  3. Subclass inherits the abstract class. It must implement all the defined abstract methods, otherwise the compiler will report compilation errors.

Abstract class is a class in essence, just a special class that cannot be instantiated, but it plays a very important role in the process of object-oriented design. In essence, abstract class embodies the relationship between is-a, just like the abstract class defined above. Human type is abstract human. If I define a vegetable type to inherit this type

public class CaiCai : Human
    {
        public override string Gender()
        {
            Return "male";
        }

    }

Caicai class must provide the implementation of abstract methods before it can be compiled. The generation of abstract class is an extension of object-oriented development, a solution to code reuse, and a result of code abstraction. The design idea of abstract class is bottom-up, that is to say, there should be subclasses in the design first. When subclasses increase gradually, then abstract common features to produce abstract class.

At this point, many students will ask, if I don’t use the abstract class as the parent class, it’s OK. Yes, ordinary classes can also take the place of abstract classes. But there are a few things that seem strange

  1. The parent class can also be instantiated, but the abstract methods seem strange, because these methods only have clear definitions in the subclass. For example, if the human class in the above code is modified to a normal type, what content should the method gender () return?
  2. During compilation, if the subclass does not implement the method of the parent class, it will not report an error, which increases the difficulty of troubleshooting. If there are many methods that need to be rewritten, troubleshooting will be very troublesome
  3. If an abstract parent class can be instantiated, it is against the idea of object-oriented in essence. After all, a parent class is an abstract concept, and what it represents after being instantiated is confusing

Interface

The most important role of interface in system design is decoupling. You should have heard “interface oriented programming” and dependency inversion more than once, which is also an embodiment of the idea of object-oriented design. An interface is essentially an abstract behavior of an object, or a contract. In interface oriented development, callers don’t care about the implementation of interface, but depend on the definition of interface. The stability of interface definition represents the stability of a system. If there is a problem with the external interface definition of a system, the system will be dead.

public interface IHuman
    {
        //Interface behavior definition
        void Walk();
    }

The above is just a simple definition of interface. The abstraction of interface can be as small as the behavior abstraction of an object, as large as the behavior abstraction of a service, and more likely as the behavior abstraction of a system. Therefore, interface is a very general concept, but it also reflects the object-oriented design concept in essence. Since interface is the definition of behavior, it has the following characteristics

  1. Interfaces can only define behaviors, not include their implementations
  2. When a type inherits an interface, it must implement all the behaviors of the interface
  3. Interfaces are different from classes and cannot contain properties

Because the interface embodies the code of conduct, the object-oriented design concept can also be used when defining the interface. When multiple different interfaces define the same behavior, we can consider abstracting a higher level interface to realize the reuse of behavior.

Write at the end

Abstract classes and interfaces are generated by the abstract behavior of objects, but abstract classes focus more on the is-a relationship, which realizes code reuse, while interfaces focus more on the abstract behavior (has – a). For example, a very simple chestnut, how to design a bird’s abstraction? Different birds may have different colors of feathers. Attributes like this can use abstract classes. Different birds may have different flight behaviors. In this way, the abstraction of behavior classes is more appropriate by using interfaces.

Whether it is an interface or an abstract class, it reflects the relationship between the upper and lower levels in the code level. Even if a programming language does not provide the definition of interface and abstract class, as long as it can realize the relationship between the upper and lower levels of objects, object-oriented programming can also be realized in principle. The abstract idea of programming is always around the top and bottom, and the internal and external dimensions are evolving reasonably.

When it comes to the definition of interface, we can also generalize it. There is only the definition of behavior method in interface. In some programming languages that do not support interface, we can regard the class that only contains method as the abstract definition of interface, which makes sense in design philosophy.

In the inheritance level and design process, abstract class is a bottom-up design idea. First, there is the code of subclass. When the subclass increases gradually, the higher level parent class will be abstracted. Different from interfaces, interface oriented programming is a top-down design idea, which abstracts the behavior contract first, and then implements it.

More wonderful articles

Working for many years, you may not really understand interfaces and abstract classes

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]