"You must know. NET"--object-oriented programming: interfaces and abstract classes (Ⅱ)

Source: Internet
Author: User
Tags define abstract

This section mainly introduces:

Object-oriented thinking-polymorphism;

Interface

Abstract class.

Ⅰ, Interface:

An interface is an abstract type that contains a set of virtual methods, each of which has its name, parameter, and return value type. The method in the interface cannot contain any implementations.

Ex  

         Public Interface Ihelloworld        {            bool checkfileformat (string  fileName);        }

 A class can implement multiple interfaces, and when a class implements an interface, it not only implements all the methods defined in that interface, but also implements all the methods that the interface inherits from other interfaces!

Ⅱ, abstract class:  

Abstract classes provide multiple derived classes that share a common definition of a base class that can provide both an abstract method and a non-abstract method. Abstract classes cannot be instantiated, and they must be implemented by their derived classes to implement their abstract methods, so the new keyword cannot be used for abstract classes and cannot be sealed. If a derived class does not implement all of the abstract methods, the derived class must also be declared as an abstract class. In addition, implementing an abstract method is done by the override method.

Ⅲ, rules and occasions:  

A. " Interface unchanged ", when the expansion by the interface, you should add a new interface, and cannot change the existing interface;
B. As far as possible to design the interface function as a single function block;
C. In the interface, all methods are default to public;
D. You cannot use the static or virtual keyword in an abstract method declaration.

Ⅳ: Similarities and differences:

1. The same point

A. Both cannot be instantiated directly, and can be implemented by means of inheritance.
B. Are the technical basis of object-oriented programming, and realize a lot of design patterns.

2. Different points

A. The interface supports multiple inheritance; the abstract class cannot implement multiple inheritance;  (strictly speaking, the interface inheriting class should be called the class implementation interface)
B. Interfaces can only define abstract rules; Abstract classes can define rules as well as provide implemented members;
C. An interface is a set of behavior specifications; Abstract class is an incomplete class, emphasizing the concept of family;
D. Interfaces can be used to support callbacks, and abstract classes have limitations when implementing callbacks;
E. Abstract classes can define methods that are implemented;
F. Interfaces can be used for value types and reference types, and abstract classes can only be used for reference types. For example, a struct can inherit an interface and not inherit an abstract class.

 

  

"You must know. NET"--object-oriented programming: 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.