Object-oriented abstract classes and interfaces

Source: Internet
Author: User

When learning C + + two years ago, know the abstract class such a concept, was not very understanding at that time, now in learning C # and design patterns, once again encountered this ambiguous things, but also in C # to learn the interface technology, interface has been heard, But the interface that was understood at the time was one or a set of functions or methods that could manipulate the data in the class. Although the teacher has given many examples, such as television this thing, its external interface is the remote control, the remote control a lot of buttons, the button below is a variety of commands or called methods.

When an abstract class encounters an interface, it is really hard to distinguish between the two concepts, so I'm going to summarize today the differences and connections between abstract classes and interfaces so that they can be understood and used profoundly.

First of all, abstract class, abstract class is a special class, is built for abstract purposes, the establishment of abstract class, is to use its polymorphic function of the member functions, to provide a unified interface for a class family. An abstract class is at the top of a class hierarchy, and an abstract class cannot be instantiated by itself, meaning that we cannot declare an object of an abstract class, and can only generate a non-abstract derived class of an abstract class and then instantiate it through the inheritance mechanism.

For example, for us human, can be divided into Chinese, Americans, Germans and so on, the Chinese people can also distinguish between Han and ethnic minorities. A person, ta must belong to the world of a country and a nation. There is no one in the pure "abstract" sense of secession and nation. Human beings are just a highly abstract concept we create, there is no instance of human itself, but by the human "abstract class", we can get all kinds of "people" who are constantly materialized, and finally get a specific person.

Abstract classes have several points to pay attention to: first, abstract classes must not be instantiated; second, abstract methods have to be overridden by quilts; third, if the class contains abstract methods, then the class must be defined as an abstract class, regardless of whether other general methods are included. Four, when declaring an abstract class, let it have as much common code as possible, with as little data as you can.

Abstract classes typically represent an abstract concept that provides a starting point for inheritance, and must be used to inherit when designing a new abstract class, so in a hierarchical structure formed by an inheritance relationship, the leaf nodes should be concrete classes, and the branch nodes should be abstract classes.

In C # We use the abstract keyword to declare classes and methods as abstract classes and abstract methods, and abstract methods do not have a method body, in the following format:

AbstractClass class Name

{

Public abstract String Method name ()

}

Abstract classes are different in C + + and C #, and function members in abstract classes in C + + are called pure virtual functions and have no function bodies, and must be initialized to 0 at the time of declaration. The specific definition and syntax format will no longer be introduced, because our current focus is on C #.

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.