Differences between interfaces and abstract classes

Source: Internet
Author: User

Abstract classes are similar to interfaces, but they have different ideas. An interface is a member of a public class, while an abstract class is a member of an abstract class to require subclass inheritance and implementation.

Abstract class: abstract class is an abstraction of commonalities.

  1. Unlike normal classes, abstract classes must be modified using abstract.
  2. If a class contains abstract methods, the class must be abstracted (declared as an abstract class and not implemented ).
  3. The subclass that inherits the abstract class must override the abstract method of the parent class.
  4. Abstract classes cannot be instantiated.
  5. Abstract classes can be used for polymorphism.
  6. It can contain abstract methods and non-abstract methods.
  7. Members can use static, abstract, override, and virtual modifiers.

  Define abstract classes and abstract methods:

abstract  class CashSuper{   public abstract double acceptCash(double money);} 

Abstract methods using abstract classes:

class CashNormal : CashSuper{  public override double acceptCash(double money)   {     return money;   }}

Interface: an interface is an abstraction of behavior.

  1. Any non-Abstract type that implements an interface must implement all the members of the interface.
  2. The interface cannot be instantiated.
  3. Interfaces can contain events, indexers, methods, and attributes.
  4. Classes and structures can inherit multiple interfaces.
  5. The interface itself can be inherited from multiple interfaces.
  6. Methods can only be abstract methods.
  7. All members are public static final and cannot use static, abstract, override, or virtual modifiers.

Example:

interface MyInterface     {               string ID        {            set;            get;        }        string Name         {            set;            get;        }        void ShowInfo();    }

 

Differences between interfaces and abstract classes

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.