Differences between interfaces, abstract classes, abstract methods, and virtual Methods

Source: Internet
Author: User

Differences between interfaces, abstract classes, abstract methods, and virtual Methods

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:

////// Abstract class of a mobile phone ///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 ");}}
////// Two interfaces are defined below. To demonstrate that multiple interfaces can be inherited ///Public interface Ilistenmusic {void listenmusic ();} public interface IClock {void clock ();}


////// Define an object class that inherits an abstract class and multiple interfaces ///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();        }    }  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.