C # The Children of abstract classes and interfaces have great similarities in support for the definition of abstract classes, and can even be replaced with each other, therefore, many developers may choose abstract classes and interfaces when defining abstract classes. In fact, there is a big difference between the two.
First, let's talk about the similarities and differences between interfaces and abstract classes:
Similarities:
1. They cannot instantiate themselves, that is, they are all used for inheritance.
2. abstract methods in abstract classes are the same as interface methods, and cannot have a method body.
Differences:
1. the abstract class can contain fields, and the interface cannot contain fields.
2. A common method in an abstract class can have a method body, while an interface method does not have a method body.
3. Methods in the interface cannot have access modifiers. abstract methods in the abstract class are inherited and must be overwritten.
4. interfaces are more like a standard and a constraint.
5. A subclass can inherit only one abstract class, but can inherit multiple interfaces.
Next is the similarities and differences between abstract methods and Virtual Methods: abstract methods can be seen as virtual methods without method bodies.
Similarities:
1. Both abstract and virtual methods can be overwritten.
Differences:
1. abstract and virtual keywords are different.
2. There must be no method body for abstract methods, but virtual methods can have a method body.
The following is an example:
/// <Summary> /// abstract class of a mobile phone /// </Summary> public abstract class mobilephone {private string _ logo; public String logo {get {return _ logo;} set {_ logo = value ;}// abstract method public abstract void call (); // virtual method Public Virtual void callagain () {console. writeline ("Call Me Again ");}}
<PRE name = "code" class = "html"> /// <summary> /// two interfaces are defined below, to demonstrate that multiple interfaces can be inherited /// </Summary> Public interface ilistenmusic {void listenmusic () ;}public interface iclock {void clock ();}
/// <Summary> /// defines an object class and inherits an abstract class and multiple interfaces. /// </Summary> public class Nokia: mobilephone, iclock, ilistenmusic {public override void call () {console. writeline ("Nokia phone no problem");} public void clock () {console. writeline ("Nokia mobile phones also have the alarm function");} public void listenmusic () {console. writeline ("Nokia phones can still listen to songs");} public override void callagain () {console. writeline ("Forget it, or stop ");}}
I believe that through the above code, we can see the difference between the two! Let's take a look at a small example!
class Program { static void Main(string[] args) { B5 b5 = new B5(); b5.MethodB(); } } public class A5 { public virtual void MethodA() { Console.WriteLine("A5.MethodA"); Console.Read(); } public virtual void MethodB() { MethodA(); } } public class B5 : A5 { public new void MethodA() { Console.WriteLine("B5.MethodA"); Console.ReadLine(); } } class Program { static void Main(string[] args) { B5 b5 = new B5(); b5.MethodB(); } } public class A5 { public virtual void MethodA() { Console.WriteLine("A5.MethodA"); Console.Read(); } public virtual void MethodB() { MethodA(); } } public class B5 : A5 { public new void MethodA() { Console.WriteLine("B5.MethodA"); Console.ReadLine(); } }
Differences between interfaces, abstract classes, abstract methods, and virtual Methods