C # Abstract, vitrual, Override

Source: Internet
Author: User

Abstract

The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can be a base class only for other classes. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from an abstract class.

1. Abstract class:

Classes that are decorated with it are called abstract classes, and abstract classes cannot be instantiated.

Abstract classes, including abstract methods, can also have abstract properties. (Abstract properties must also be rewritten.) )
Non-abstract classes derive from abstract classes and must be rewritten to implement all inherited abstract methods.

If a class derives from an abstract class, but does not implement a method in an abstract class, then the class must be an abstract class. That is, derived classes are also abstract classes.

2. Abstract Method:

Abstract methods can only be declared in abstract classes. Static and virtual modifications can no longer be used

The abstract method only gives the prototype of the method. You cannot include any executable code in an abstract method, without a method body, ending with a semicolon.

abstract methods cannot be accessed using the base keyword in a derived class.  

    Class program    {        static void Main ()        {            DeriverC2 tt =new DeriverC2 ();       Instantiate the Deriverc class, the abstract class cannot instantiate            Tt.function1 ();        }    }                         abstract class Basec//abstraction classes;    {public        abstract void function1 ();        Abstract class    derivec1:basec              //abstract class inherits abstract class;    {public        void function2 () {}              non-abstract method in abstract class;    }    Class Deriverc2:basec                      //non-abstract class inherits abstract class;    {public        override void Function1 ()         //must be rewritten;        {            Console.WriteLine ("Class DeriverC2 specifically implements the abstract method of Class Basec function1 ()");}    }

Virtual

The virtual keyword is used to decorate a method, property, indexer, or event declaration and to allow these objects to be overridden in a derived class.

The implementation of a virtual member can be changed by an overriding member in a derived class. For example, this method can be overridden by any class that inherits it.

By default, the method is non-virtual. Cannot override non-virtual methods

Override

Override keyword to extend or modify an abstract implementation or virtual implementation of an inherited method, property, indexer, or event, you must use the override modifier.

The override method provides a new implementation of a member inherited from a base class.

Overridden by the Override declaration method is called overriding the base method. The overridden base method must have the same signature as the override method.

You cannot override a non-virtual method or a static method. The overridden base method must be virtual, abstract, or override.

The override declaration cannot change the accessibility of the virtual method. The override method and the virtual method must have the same access level modifier. You cannot use the modifier new, static, virtual, or abstract to modify the override method.

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

Summarize:

Abstract keywords can be used to modify classes, abstract classes cannot be instantiated, abstractions can inherit abstract classes.

The virtual keyword is decorated with the same point as the abstract method, which uses the Override keyword to override the method, except that the abstract method must exist in the abstract class, and the subclass must be overridden, unless the subclass is abstract.

The override keyword, overriding the base method must be the same as the override method name, the property can also be overridden, the overridden property and the inherited property have the same access modifier, type, and name.

C # Abstract, vitrual, Override

Related Article

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.