C # Interface and abstract class comparison learning

Source: Internet
Author: User
Tags abstract definition comparison inheritance modifier

What is an interface?

An interface is a specification, a protocol (*) that allows you to write common code by agreeing to a specification.

Defines a set of methods with various functions. An interface describes an ability that can have nothing to do with that ability. Like what:

Public interface Ifly{void fly ();}

The interface can be achieved by airplanes, birds, and so on, they all have the ability to fly but it doesn't matter. and the abstract class is different.

Interface solves the problem of multiple inheritance of a class

Interface solves the problem of large volume after class inheritance.

Multiple inheritance can be implemented between interfaces

The member in the interface must not have an implementation (not implemented plainly is not the curly braces OH), the interface cannot be instantiated.

The member in the interface cannot have an access modifier, an implicit public

Interface can have properties, methods, indexers, etc. (in fact, are all methods, you can use reflector to see), but can not have fields

All members in the interface must be fully implemented in the quilt class

The characteristic summary of the interface

1. Interface is a specification. For polymorphism.

2. The interface cannot be instantiated.

3. The members in the interface cannot be added with the access modifier, and the member access modifiers in the interface are public.

Cannot be modified. (Default is public)

4. The members of an interface cannot have any implementations ("Say no," but define a set of not-implemented members).

5. An interface can have only methods, properties, indexers, events, and no fields.

6. Interfaces and interfaces can be inherited, and can be more inherited.

7. The subclass that implements the interface must implement all the members of the interface.

8. A class can inherit a class at the same time and implement multiple interfaces, if a subclass inherits the parent class A at the same time,

and implements the interface IA, then syntactically a must be written in front of IA. Class myclass:a,ia{}, because the classes are single inherited.

9. When an abstract class implements an interface, you can implement the member as abstract if you do not want to implement the member in the interface. (An abstract class can also implement an interface, marked with Abstrac)

10. "Show implementation Interface" can only be invoked through interface variables (because the member is private after the display implements the interface).

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

At programming Time:

interface → Abstract class → parent class → concrete class (You can use abstractions when defining method parameters, return values, declaring variables). )

You can use an interface without abstract classes, you can use abstract classes without classes, you can use the parent class without subclasses.

Avoid the definition of "bulky interface", "Multi-function interface", will cause "interface pollution." Define only the associated group of members to a single interface (try to define members less in interfaces). Principle of single responsibility

Define multiple, single interfaces (small interfaces) (used in combination).

The difference between an abstract class and an interface

interface defines the public behavior of a class, and the abstract class defines the public implementation of the class

A class can only inherit from one class (abstract class), but can implement multiple interfaces at the same time

An interface cannot have an implementation, an abstract class can have members that are not implemented or can have implementations

A method that is not implemented in an interface must be implemented in a subclass, and the members that are not implemented in an abstract class must be overridden in a subclass

A good interface definition should be functional rather than versatile, otherwise it will cause interface contamination.

If a class only implements one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution. Such as:

public interface  ifly
    
{
    
    void Fly ();
    
     void Eat ();
    
  The
    
classes plane:ifly
    
{
    
    //class implements an interface that must implement all its members that are not implemented, so the aircraft must have a eat approach. If you say that the plane eats oil, there is no way.
    
     void Fly () {...};
    
       void Eat () {...}

When to use an abstract class and when to use an interface

Abstract classes are used primarily for closely related objects, and interfaces are suitable for providing common functionality for unrelated classes.

Author: wolfy

Source: http://www.cnblogs.com/wolf-sun/

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.