C # Inheritance in abstract, virtual, override, and new

Source: Internet
Author: User
Tags modifiers

Abstract

Detailed reference: https://msdn.microsoft.com/zh-cn/library/sf985hc5.aspx

The abstract modifier indicates that the modified content is missing or not fully implemented. the abstract modifier is available for 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.

abstract class
-abstract class cannot be instantiated. The
-abstract class can contain abstract methods and abstract accessors.
-not   sealed (C # Reference) The   modifier modifies the abstract class, because the meanings of the two modifiers are reversed.     sealed   modifier cannot inherit, but   abstract The   modifier requires that the class be inherited.
-a non-abstract class derived from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.

Abstract Methods has the following characteristics:
-the abstract method is an implicit virtual method.
-Allows abstract method declarations to be used only in abstract classes.
-because the abstract method declaration does not provide the actual implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly braces ({}) after the signature.
-implemented by an overriding method Override (C # Reference) provided, this override method is a member of a non-abstract class.
-Using the static or virtual modifier in an abstract method declaration is an error.

Virtual

Detailed reference: https://msdn.microsoft.com/zh-cn/library/9fkccyh4.aspx

The virtual keyword is used to decorate methods, properties, indexers, or event declarations, and to make them overridden in derived classes. For example, this method can be overridden by any class that inherits it. when a virtual method is called, the runtime type of the object is checked for the overriding member. The overriding member in most derived classes is called, and if no derived class overrides the member, it may be the original member.  By default, the method is non-virtual. Non-virtual methods cannot be overridden. the virtual modifier cannot be used with static,abstract, private , or override modifiers.

In C #, the name of a method in a derived class can be the same as the name of a method in a base class. You can specify how the method interacts by using the new and override keywords.  The override modifier extends the base class method, and the new modifier "hides" it. Reference: Https://msdn.microsoft.com/zh-cn/library/ms173153.aspx

Override

Detailed reference: https://msdn.microsoft.com/zh-cn/library/ebca9ah3.aspx

to extend or modify an abstract implementation or virtual implementation of an inherited method, property, indexer, or event, you must use theOverridemodifier. The override method provides a new implementation of a member inherited from a base class. the method overridden by the override declaration is called the overriding base method.  The overridden base method must have the same signature as the override method.
-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. override   methods and   virtual   method must have the same access level modifier.

-You cannot modify the override method with the new,static , or virtual modifiers.

New

reference: https://msdn.microsoft.com/zh-cn/library/435f1dw2.aspx

The new   keyword can explicitly hide members that inherit from a base class.   When you hide an inherited member, the derived version of that member replaces the base class version.   Although you can not use     modifier hides the member, but you receive a compiler warning.   new   To explicitly hide members, this warning is suppressed. Members of a base class can be replaced with new, regardless of whether or not there is a virtual declaration.

    public class Class1    {public        Class1 ()        {            Console.WriteLine ("This is Class 1");        }        Public virtual void Run ()        {            Console.WriteLine ("Class 1 is Running");        }        public void Test ()        {            Console.WriteLine ("Class 1 Test");        }    }    public class Class2:class1    {public        Class2 (): Base ()        {            Console.WriteLine ("This is Class2");        }  the virtual and non-virtual functions of the public new void Run ()//parent class can be replaced with        new {            Console.WriteLine ("Class 2 is Running ");        }        public void Test ()   //Does not add new warning        {            Console.WriteLine ("Class 2 Test");        }    }

The difference between new and override

Reference: Versioning with the Override and New Keywords (C # Programming Guide) and understanding when to use the Override and New Keywords (C # Programming Guide)

When a derived class has the same name as a method of a base class, the base class method can be defined as virtual and overridden with override in the derived class, or a method that replaces the base class with the new keyword in a direct-derived class or without (default or new but with a warning). So what are the different effects of these two cases?

The following example illustrates the important differences between them. For instances of base classes that point to a base class or instances of derived classes point to derived classes, the result is the same. However , for instances of a base class that point to a derived class, new calls the method of the base class, and override invokes the method of the derived class .

    public class Class1 {public virtual void run () {Console.WriteLine ("Class 1 is running");        } public void Test () {Console.WriteLine ("Class 1 Test"); }} public class Class2:class1 {public override void Run () {Console.WriteLine ("Clas        s 2 is Running-override ");        } Public new void Test () {Console.WriteLine ("Class 2 Test-new"); }} class Program {static void Main (string[] args) {Console.WriteLine ("Condition 1:")            ;            Class1 C1 = new Class1 ();            C1.run ();            C1.test ();            Console.WriteLine ("Condition 2:");            Class2 C2 = new Class2 ();            C2.run ();            C2.test ();            Console.WriteLine ("Condition 3:");            Class1 C0 = new Class2 ();                          C0.run ();      Derived override Run-class2 c0.test ();                   Base-class1 Console.readkey (); }    }




C # Inheritance in abstract, virtual, override, and new

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.