Learn to remember a little bit so that you do not forget it ..
I read many articles written by my predecessors about abstract class and interface, and I tried to sort them out.
Abstract class:
1. abstract classes cannot be instantiated.
2. abstract classes can have constructor methods and can be called in Their Derived classes.
3. abstract methods in abstract classes (without method bodies) must be overwritten in the derived classes. Non-Abstract METHODS (with method bodies) can not be overwritten.
4. abstract classes can contain fields, attributes, methods, constructors, and other Members.
5. abstract classes can only satisfy single inheritance for interfaces.
Code
Abstract class Myabstract
{
Public Myabstract () {Console. WriteLine ("Abstract class constructors ");}
Public abstract void abstractMethod ();
Public void Method ()
{
Console. WriteLine ("Myabstract Method ");
}
}
Interface:
1. The interface cannot be instantiated.
2. The interface cannot have constructor methods.
3. the method in the interface does not need a method body. You must override the method in the derived class.
4. The interface cannot have fields.
5. There is no modifier for the interface method, and its meaning is similar to public.
6. The interface can meet the requirements of "Multi-inheritance ".
Interface MyInterface
{
Void interfaceMethod ();
}
Class:
Code
Class MyClass: Myabstract, MyInterface
{
Public MyClass ()
: Base ()
{
}
Public override void abstractMethod ()
{
Console. WriteLine ("MyClass Override Abstract Method ");
}
Public void interfaceMethod ()
{
Console. WriteLine ("MyClass Override Interface Method ");
}
}
Main Method:
Code
Class Program
{
Static void Main (string [] args)
{
MyClass myclass = new MyClass ();
Myclass. abstractMethod ();
Myclass. interfaceMethod ();
Myclass. Method ();
}
}
Output:
Abstract Class Constructors
MyClass Override Abstract Method
MyClass Override Interface Method
Myabstract Method