[Syntax] full understanding of abstract class, abstract method, virtual method, and interface)

Source: Internet
Author: User

We are developing a. Net-based object-orientedProgramThe concept and usage of abstract classes and interfaces are often unclear. In fact, it is not difficult to understand them.

    • 1. What is an abstract class?

Abstract classes are basic types that are highly abstract and summarized to describe a type of things. This is still obscure. For example, animals are an abstract class. When we talk about animals, we can imagine many specific animal types, for example, pigs, dogs, tigers, and lions all inherit the abstract class "Animals" and implement specific sub-classes of their specific attribute features.

Abstract classes have several necessary factors:

1. abstract classes cannot be instantiated.

2. abstract classes can contain abstract methods and other accessors with abstract marks. (What is an abstract method .)

3. abstract classes are used to inherit from other classes. Therefore, we cannot use sealed to modify abstract classes. Otherwise, there will be contradictions.

4. If an abstract class contains an abstract method or other accessors with an abstract flag, you must explicitly implement these abstract methods and accessors In the subclass derived from this abstract class.

Here is an example from msdn:

 

Abstract class sample1

  Abstract    Class  Mybasec  //  Abstract class  
{
Protected Int X = 100 ;
Protected Int Y = 150 ;
Public Abstract Void Mymethod (); // Abstract Method

Public Abstract Int Getx // Abstract Property
{
Get ;
}

Public Abstract Int Gety // Abstract Property
{
Get ;
}
}

Class Myderivedc: mybasec
{
Public Override Void Mymethod ()
{
X ++ ;
Y ++ ;
}

Public Override Int Getx // Overriding Property
{
Get
{
Return X + 10 ;
}
}

Public Override Int Gety // Overriding Property
{
Get
{
Return Y + 10 ;
}
}

Public Static Void Main ()
{
Myderivedc MC = New Myderivedc ();
MC. mymethod ();
Console. writeline ( " X = {0}, y = {1} " , MC. getx, MC. Gety );
}
}

Mybasec is an abstract class. Because it has abstract modification, it contains an abstract method mymethod. Note that because it is an abstract method in an abstract class, we do not need to implement it, just like the method defined in the interface. It also has two abstract attributes. Similarly, we do not need to implement them.

In fact, we can see the difference between abstract method and virtual method. In fact, an abstract method is an implicit virtual method, but it only appears in the abstract method and does not need to implement the specific method content.

Myderivedc is derived from the abstract class mybasec. The first abstract method and two abstract attributes we saw just now must be implemented using override in this subclass.

    • 2. Understanding of the interface.

I will not describe the concept of interfaces here. The key is how to understand interfaces. We can regard the interface as an abstraction of "can do something. For example, birds and planes are two different types, but birds fly and planes fly. They both fly "can fly ", but they fly in different ways, but they actually achieve the same purpose. Therefore, we can write a flying interface so that both classes can achieve the goal of flying.

Why? This is an object-oriented programming technique.CodeReadability and scalability. We define interfaces, just like we have a contract, contract, or standard. We must strictly follow this contract to do things, so that we can achieve a better degree with rules, so that we can write code in a more organized and efficient manner.

    • Iii. Suggestions on abstract classes and interfaces.

In fact, here we will all find that abstract classes and interfaces are very similar in implementing a method mechanism. They both set up a treaty first, and then subclass must implement them. How can we use them? First, let's look at their differences:

    1. When the agreed content needs to be passed as parameters, we need to use interfaces. abstract methods in abstract classes are not that convenient.
    2. A subclass can inherit only one abstract class, but can inherit multiple interfaces. If we have a group of classes that need to implement the same method, if we use abstract classes, then the base classes of all these subclasses must be these abstract classes.
    3. Abstract classes can have specific implementation methods or other Members, but the interface is not good, and members in the interface cannot have specific implementation.

Here are some official suggestions:

    1. If you want to write components with multiple versions, or the components you write must be updated frequently, you are advised to use abstract classes. Because we can update our components by updating the base class in a simple way. But the interfaces are different. The interfaces are like contracts. Once an interface is established, it is difficult to change it. If we need a new version of the interface, we need to re-write this interface. Because we cannot update subclasses that implement these interfaces by changing interfaces.
    2. If the features we designed can be applied to objects with considerable differences, we recommend using interfaces. Abstract classes are applicable to the functions to be implemented between objects with smaller differences.
    3. If we need to design simple and small functions, we use interfaces. If we need to design large functional units, use abstract classes.
    4. If we need to provide a common and implemented functional module used in all components, we can use abstract classes. abstract classes can partially implement members, but interfaces cannot implement any members.

To sum up, we believe that we can have a comprehensive understanding of abstract classes and interfaces. Write it here today.

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.