C # Supplements-Interfaces and abstract classes

Source: Internet
Author: User

Constructors can be used in abstract classes (no parameter constructors and parametric constructors)
The parameterless constructor is called when the subclass is instantiated
The parameter constructor must show the calling

Abstract classes can have abstract methods but cannot have method bodies, subclasses must implement abstract methods
Subclasses must override abstract methods in abstract classes and cannot use the new

There can be virtual methods in abstract classes, and virtual methods must have method bodies
Subclasses inherit abstract classes and can override (override) and overwrite (new) virtual methods in abstract classes or do nothing.
But the two (override, new) can not be done at the same time, can only choose one

     Public Abstract classC { Public stringStrabs; protectedC () {Console.WriteLine ("Abstract class C"); }        //The subclass display call does not execute a parameter constructor        protectedCstringstr) {Strabs=str; }         Public voidMethod1 () {Console.WriteLine ("Abstract Class:method"); }        //Public void Method2 (); Methods in an abstract class must have a method body         Public voidMethod2 () {//there is a method body, can not be specifically implemented        }        //Abstract classes can have abstract methods, but abstract methods cannot have method bodies, that is, there is no implementation part         Public Abstract voidMehod3 (); //Public virtual void Method4 (); Incorrect notation: virtual methods in abstract classes must have method body         Public Virtual voidMethod4 () {Console.WriteLine ("Abstract class virtual method"); }    }     Public Abstract classE:c {/// <summary>        ///subclasses are instantiated with the parent class without the parameter constructor./// </summary>        protectedE () {Console.WriteLine ("Abstract class E"); }        /// <summary>        ///The subclass display call does not execute a parameter constructor/// </summary>        /// <param name= "str" ></param>        protectedEstringstr) {             This. Strabs =str; }        //subclasses inherit abstract classes, you must override abstract methods in abstract classes         Public Override voidMehod3 () {}//subclasses inherit abstract classes, and for the virtual methods of abstract classes, either override (override) or overwrite with new, they can only be selected         Public Override voidMethod4 () {Console.WriteLine ("E:c override Method"); }        //Public new void Method4 ()//{        //Console.WriteLine ("E:c New Method"); //}    }     Public classF:e {//Show call parent class with parameter constructor         PublicFstringSTR):Base(str) {Strabs=str; }    }
Instance Code

Methods in interfaces cannot have method bodies and cannot have abstract methods
An interface is a set of behavior specifications and rules
Subclasses implement interfaces and must implement all the methods in the interface
So when defining an interface, the rules in the interface are as simple as possible, so that an interface is only "one class" rule
This also shows that the interface is highly abstract, highly generalized, and truly seek the same "de-xor"


The role of abstract classes
Abstract classes are primarily used for code reuse
Usage of the interface
The use of interfaces mainly embodies the object-oriented polymorphism

C # Supplements-Interfaces and abstract classes

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.