Interface and abstract class

Source: Internet
Author: User

Summary:

1. Interface

There is only a method name and no method body. The class inherited from it provides the implementation of all methods. In C #, the class can only be individually inherited, but multiple inheritance can be implemented through interfaces, that is, a class can inherit multiple different interfaces.

2. Define an Interface

A. the constructor cannot be defined.

B. instance members cannot be defined.

C. Static members cannot be defined.

3. Call the Interface Method

For example: public sealed class String: IComparable, ICloneable, IConvertible, IComparable <string>, IEnumerable <char>, IEnumerable, IEquatable <string>

String inherits multiple interfaces. Taking ICloneable as an example, the code is as follows:

String ss = "chine ";
ICloneable cloneable = ss;

---- The cloneable variable can only access the methods defined in ICloneable and the methods inherited from the upper part of the inheritance chain [Methods in Object]. The cloneable variable cannot access any methods in the String type.

4. value types and interfaces

Similar to the reference type, the value type can implement zero or multiple interfaces. However, when the converted value type is an interface type, the packing operation will occur, resulting in performance loss.

5. implementation of explicit interfaces (EIMI: explicit interface method implementation)

Public interface ITouch {void Too ();} public class Dosome: ITouch {public void Too () {Console. writeLine ("public Too");} void ITouch. too () // explicit interface implementation. You cannot add any access modifiers (such as public and private) {Console. writeLine ("ITouch Too") ;}---- call: Dosome dosmoe = new Dosome (); dosmoe. too (); // output: public Too ITouch itouch = dosmoe; itouch. too (); // output: ITouch Too

---- The explicit interface can only be accessed through the interface type variables, and the instance variables of the implementation class of this interface cannot be accessed.
---- Use EIMI as little as possible

6. Similarities and Differences between interfaces and abstract classes

A. Similarities

They cannot be directly instantiated. They all implement their abstract methods through inheritance classes.

B. Differences

The interface supports multi-inheritance; the abstract class does not support multi-inheritance.

An interface only contains methods, attributes, indexers, and event signatures, but cannot define fields or methods that contain implementations. An abstract class can define fields, attributes, and methods that contain implementations.

7. When to use interfaces and when to use abstract classes [rules and occasions]

A. abstract classes are mainly used for closely related objects, and interfaces are suitable for providing general functions for irrelevant classes.

B. The interface focuses on the CAN-DO relationship type, while the abstract class focuses on the relationship of the IS-A type.

C. If a version problem is expected, you can create an "abstract class ". For example, if you have created a Dog, a Chicken, and a Duck, you should consider Abstracting an Animal to deal with other types of animals in the future. Adding a new member to an interface requires you to modify all the derived classes and re-compile them. Therefore, it is best to use an abstract class to solve the version problem.

D. Non-abstract classes derived from abstract classes must include all the inherited abstract methods and the implementation of the abstract accessors.

E. The abstract class cannot use the new keyword or be sealed because the abstract class cannot be instantiated and can only be implemented through the inheritance class [if it is sealed, no subclass can be created ].

F. static or virtual modifiers cannot be used in abstract method declarations.

G. If you want to design small and concise functional blocks, use interfaces. If you want to design a large functional unit, use an abstract class.

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.