Interface, abstract class, virtual method, rewrite (interface, abstract class, virtual function, override)

Source: Internet
Author: User
Document directory
  • 1. Let's talk about polymorphism first.
1. Differences between abstract classes and virtual functions (abstract, virtual function)

The two words abstract and virtual are a little abstract and confused. In fact, they are easy to differentiate.

1.Abstract: you can modify the members of a class or a class. in addition, the class must be abstract modified before the members can be abstract modified. virtual cannot be used to modify the class. it can only be used to modify members. virtual can be used in a general class or abstract class modified by abstract.

2.Abstract classes cannot be instantiated. For example, defining an abstract class dosomething () {}; you can't use it like this: dosomething DO = new dosomething ();

Abstract classes must be inherited and overwritten to be instantiated. there is nothing in the abstract method. there is only one method defined in it. It cannot implement any function in it, and it must be rewritten in the inherited subclass before it can be used. the virtual method cannot be implemented only by definition. it can be rewritten and reused in the inherited subclass, or used directly without rewriting. The usage is the same as that of the general method.

2. Why do we need to develop abstract classes, abstract methods, and virtual methods?

Virtual and abstract methods can be used to achieve polymorphism. abstract classes are similar functions to interfaces.

1. Let's talk about polymorphism first.

We know that the three main features of object-oriented are encapsulation, inheritance, and polymorphism. polymorphism simply means that the same method (that is, the return type, method name, and parameter list are all the same) is called by inheriting multiple classes created .) different functions are implemented. it is implemented through the same override method in inheritance.

In addition, there is a word called overload, which means that only the parameter names are the same. The parameter list must be different.

Here is a simple example of polymorphism:

// Base class:

Public class person

{

Public Virtual Void Do ()

{

Console. writeline ("do something ");

}

}

// Inherited class:

Public class cheater: person

{

Public override Void Do ()

{

Console. writeline ("go to take money from some fools ");

}

}

Public class killer: person

{

Public override Void Do ()

{

Console. writeline ("go to kill somebody ");

}

}

Class persondo -- test class
{
Static void main (string [] ARGs)

{

Person cheater = new cheater ();

Cheater. Do ();

Person killer = new killer ();

Killer. Do ();

}
}

-- Result: Go to take money from some fools

-- Go to kill somebody


You can also change the base class to an abstract class without changing the inheritance subclass. The running result is the same as the above.

Public abstract class person

{

Public abstract Void Do ();

}

Ii. Differences between abstract classes and interfaces:

The interface is similar to the abstract class:

1. It cannot be instantiated. 2. It has an unimplemented method. 3. It can be instantiated only when the inherited subclass has implemented an unimplemented method.

Differences:

1. abstract classes are also a special class. All classes can be inherited only by a single interface.

2. Methods in the interface cannot be modified by public or private modifiers. The default value is public.

3. In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members.

4. Methods in the interface cannot be implemented. abstract classes can implement some or all methods, just like general classes.

Abstract classes reflect the concept of classes, Abstract of a certain type of things, attributes of such things, and various methods. interfaces are a set of common operations. for example, all animals have sleep and eat methods. you can define an interface with sleep and eat methods. birds have attributes, two feet, and feathers. there is a way to fly, egg. you can define an abstract class.

In fact, it is only for the convenience of design, clear and easy to understand to do so. if you want to confuse the interface with the abstract class without any difference, it is also possible. pay attention to the minor syntax differences.

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.