Interface and abstract class comparison

Source: Internet
Author: User
Tags define abstract

1. Interface-interface

1.1 Interface declarations start with I by default

1.2 Interface definition only declares methods, does not implement concrete method body

1.3 Interface defaults to public

1.4 Interface definition behavior (constraint behavior)

     Public Interface // Defining Interfaces     {        void  usermobile ();    }      Public class // Inheriting Interfaces     {        publicvoid// Implement Interface         {            Console.WriteLine ();        }    }

2. Abstract class-abstract

2.1 You can define abstract classes without implementing a concrete method body (abstract classes can have attributes, implemented methods, non-implemented methods)

There are abstract methods in class 2.2, and this class must be an abstract class

Class 2.3 and abstract classes can inherit abstract classes

2.4 Abstract methods in abstract classes if you inherit an abstract class

     Public Abstract classTestName//Abstract class    {         Public stringName {Get;Set; }//Properties         Public Abstract voidPlay ();//abstract method, method body not implemented         Public voidEat ()//implemented Methods{Console.WriteLine (); }    }     Public classTextnamechild:testname//inheriting abstract classes (inheriting abstract classes must implement abstract methods)    {         Public Override voidPlay ()//Rewrite play class{Console.WriteLine (); }    }

3. Differences

3.1 Abstract class can inherit only one abstract class, interface can inherit multiple interfaces

4. Instantiation

New Imeimobile (); // instantiation of the interface * * = new implements the class name of the interface ();  New textnamechild (); // instantiation of an abstract class = new implements the class name of the abstract class ();

Interface and abstract class comparison

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.