SoFunction
Updated on 2025-03-07

Personal summary of the difference between classes and interfaces in C#

1. The difference between classes and interfaces

Class: Describes an entity, including the state of an entity, and also includes actions that an entity may issue.

Interface: defines actions that an entity may issue. But it only defines the prototype of these actions, without implementation, and without any state information.

1. Differences between interfaces and classes:

(1) Interfaces are a bit like a specification or a protocol, and are an abstract concept;

(2) Class implements this protocol and satisfies the specific entity of this specification, which is a specific concept.

(3) From a program perspective, simply understand that interfaces are function declarations and classes are function implementations. It should be noted that there may be many implementations of the same declaration.

2. The difference between interface and abstract classes:

(1)A class can implement any number of interfaces but can subclass at most one abstract class.

(2)An abstract class can have nonabstract methods, which are usually instance of the TEMPLATE METHOD pattern. All the methods of an interface are abstract,

whether or not this declaration is explicit.

(3)An abstract class can declare instance variable that its subclass inherit. An interface cannot declare instance variables, although it can establish static final fields.

(4)An abstract class can define constructors, an interface cannot.

(5)An abstract class can have methods whose visibility is protected, private, or none(package);every method in an interface must be public.

(6)An abstract class inherits form Object, including such method as clone() and equals().

2. The use of interfaces is mainly reflected in the following aspects:

(1) The same behavior of unrelated classes can be realized through the interface, without understanding the corresponding class to the object.

(2) Through the interface, you can specify the methods that multiple classes need to implement.

(3) Through the interface, you can understand the object's interactive interface without understanding the corresponding class to the object.

3. Some interface questions:

1. What I saw from the book is that only constants and empty methods are defined in the interface of a java program. What is the use of an empty method? You also need to write the method body in the class, so what else do you need to do with the interface?

Answer: The prototype of the class method is defined in the interface, but it cannot be said to be an empty method, because the empty method means there is an implementation body, but the implementation body is an empty operation. In fact, the interface does not define any implementation body. The specific implementation bodies are all in the class that implements the interface, and the interface only defines the calling method of these methods.

Of course, you can also write methods directly in the class without using interfaces, but if your set of methods need to be implemented in many classes, wouldn’t it be better to abstract them and make them into an interface specification?

2. There are 2 interfaces written in a program and the method body is written in the same class. What does this have to do with multiple inheritance?

Answer: A class describes an entity. This entity may be a complex object and has many actions. If you classify these actions, use interface a to define a certain set of actions, and interface b to define another set of actions, this structure is clearer.

This method has the advantages of multiple inheritance and avoids the defect of multiple inheritance. In fact, historically, interfaces were largely designed to solve various problems brought about by multiple inheritance.

3. How do you know what methods are defined in the package?

Answer: The input and output of the method are defined in the interface. These are protocols, and the specific implementations are in each class. For many places where only abstract interfaces are needed, you don’t need to know what the specific class is, as long as this class implements this interface.