Abstract (abstract) classes and interface (interfaces) are two concepts in object-oriented programming, but programmers who have studied programming for many years are sometimes still not clear about the distinction between these two concepts. The concepts and differences between the abstract class and the interface are explained below.
Abstract (abstract) class
Abstract methods are only declarations, not implementations, and can be viewed as virtual methods without a body.
Abstract classes cannot be instantiated.
Abstract classes can but do not have to have abstract attributes and abstract methods, but once you have an abstract method, you must declare the class as an abstract class.
A specific derived class must overwrite the abstract method of the base class.
Abstract derived classes can override the abstract methods of the base class, or they can be overridden. If they are not overridden, their specific derived classes must overwrite them.
The following code explains how abstract (abstract) classes are used.
Using System;
public abstract Class A//abstract classes A
{
private int num = 0;
public int Num//abstract class contains attributes
{
Get
{
return num;
}
Set
{
num = value;
}
}
public virtual int Getnum ()//abstract class contains virtual methods
{
return num;
}
public void Setnum (int n)////abstract class contains common methods
{
This.num = n;
}
public abstract void E (); Abstract method in Class A E
}
Public abstract class B:A//Because Class B inherits the abstract method E from class A, Class B also becomes an abstract class
{
}
public class C:b
{
The public override void E ()//Overrides an abstract method inherited from Class A. If Class B itself also defines an abstract method, you must also override the
{
throw new Exception ("the method or operation isn't implemented.");
}
}
public class Test
{
static void Main ()
{
c C = new C ();
C.E ();
}
}
Interface (interface)
interface cannot be instantiated.
An interface can contain only method declarations.
The members of an interface include methods, properties, indexers, events.
All members in an interface default to public, so there cannot be a private modifier in an interface.
A derived class must implement all members of the interface.
A class can implement multiple interfaces directly, separated by commas.
An interface can have more than one parent interface, and the class that implements the interface must implement all members of all the parent interfaces.
An interface cannot contain constants, fields (fields), constructors, destructors, static members.
The following code explains how to use the interface (interface).
public delegate void EventHandler (ob ject sender, Event e);
public interface ITest
{
int x = 0;
int A
{
Get
Set
}
void Test ();
Event EventHandler event;
int This[int index]
{
Get
Set
}
}
The same points and differences between abstract classes and interfaces
Same point
Can be inherited
cannot be instantiated.
Can contain method declarations
Derived classes must implement methods that are not implemented
Difference
Abstract base classes can define fields, properties, method implementations. Interfaces can only define properties, indexers, events, and method declarations, and cannot contain fields.
An abstract class is an incomplete class that needs further refinement, whereas an interface is a behavior specification.
Interfaces can be implemented multiple implementations, and abstract classes can only be inherited by a single inheritance.
Abstract classes are more defined in a series of tightly related classes, while interfaces are mostly loose but implement a functional class.
Abstract class is a concept abstracted from a series of related objects, so it reflects the internal commonness of things. Interfaces are a functional convention defined to satisfy an external invocation, thus reflecting the external nature of things.
Interfaces do not basically have any specific characteristics of inheritance, and it only promises a method that can be invoked.
Interfaces can be used to support callbacks, and inheritance does not have this feature.
The concrete method implemented by the abstract class defaults to virtual, but the interface method in the class that implements the interface is not virtual by default, but you can also declare it as virtual.
If an abstract class implements an interface, you can map the methods in an interface to an abstract method in an abstract class without having to implement them, and implement the methods in an interface in a subclass of an abstract class.
Abstract class and interface usage rules
Abstract classes are primarily used for closely related objects, while interfaces are best suited for providing common functionality for unrelated classes.
If you want to design a large functional unit, use an abstract class, or use an interface if you want to design a small, concise feature block.
If you expect to create multiple versions of a component, you create an abstract class. An interface cannot be changed once it is created. If you need a new version of the interface, you must create an entirely new interface.
If you create a feature that will be used across a wide range of objects, use an interface, or use an abstract class if you want to provide common implemented functionality between all of the components ' implementations.
Analyze the object, refine the internal commonness to form an abstract class, to express the object's essence, namely "what". The interface is preferred for external supply calls or extensions to functionality.
A good interface definition should be functional rather than versatile, otherwise it will cause interface contamination. If a class only implements one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution.
As far as possible to avoid using inheritance to implement the build function, but use the black box multiplexing, that is, the combination of objects. Because of the increase in the level of inheritance, the most immediate consequence is that when you call a category in this class group, you must load them all into the stack! The consequences are conceivable. (combined with stack theory). At the same time, interested friends can note that Microsoft is building a class, many times the use of the method of object composition. For example, in ASP.net, the page class has attributes such as server request, but in fact they are all objects of a class. Using this object of the page class to invoke the methods and properties of another class, this is a very basic design principle.