Differences between abstract classes and interfaces

Source: Internet
Author: User

1. An abstract class can still be implemented: it can have members, non-abstract methods or attributes, and the interface cannot have implementations and member variables.

2. A. Net class can only be derived from a base class, that is, the base class is abstract. However, A. Net class can implement multiple interfaces as needed.

3. abstract classes can be derived from other classes or from one or more interfaces, while interfaces can only be derived from another interface.

4. abstract classes can have common methods and attributes. Even if they are abstract, all the Members in the interface are not public.

5. abstract classes can contain static methods and static members, and define constants at the same time. Interfaces do not contain any of them.

Appendix: typical examples of abstract classes:

Using system;
Abstract class vehicle // defines the vehicle class
{
 Public int wheels; // Public Member: Number of wheels
 Protected float weight; // protected member: Weight
 Public Vehicle (int w, float g ){
    Wheels = W;
    Weight = g;
 }
 Public Virtual void speak (){
    Console. writeline ("the W vehicle is speaking! ");
 }
};

class car: Vehicle // defines the car type
{< br> int passengers; // Private member: number of passengers
Public Car (int w, float g, int P): Base (W, g)
{< br> wheels = W;
Weight = g;
passengers = P;
}< br> Public override void speak () {
console. writeline ("the car is speaking: di-di! ");
}< BR >}

Class truck: Vehicle // define the truck class
{
 Int passengers; // Private member: Number of passengers
 Float load; // Private member: Load
 Public truck (int w, float g, int P, float L): Base (W, g)
 {
   Wheels = W;
   Weight = g;
   Passengers = P;
   Load = L;
 }
 Public override void speak (){
   Console. writeline ("the truck is speaking: Ba-Ba! ");
 }

 

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.