C # abstract class abstraction

Source: Internet
Author: User

Classes that cannot be initialized are called abstract classes , and they provide only partial implementations, but another class can inherit it and can create instances of them, with methods that are not implemented. An abstract class cannot be a new object.

"A class that contains one or more pure virtual functions is called an abstract class, and an abstract class cannot be instantiated, and further an abstract class can only be used by interfaces and as base classes of other classes.

Abstract classes can be used for classes, methods, properties, indexers, and events, using abstract in a class declaration to indicate that the class is intended as a base class member of another class to be marked abstract, or to be included in an abstract class that must be implemented by its derived class.

Abstract classShapesclass {Abstract  Public intArea (); }    classSquare:shapesclass {intx, y; //Not providing a area method results//In a compile-time error.         Public Override intArea () {returnX *y; }    }

An abstract class that contains non-abstract methods:

Abstract classMyabs { Public voidNonabmethod () {Console.WriteLine ("non-abstract Method"); }    }    classMyclass:myabs {}classmyclient { Public Static voidMain () {//myabs MB = new Myabs ();//Not possible to create an instanceMyClass MC=NewMyClass (); Mc. Nonabmethod (); //displays ' Non-abstract Method '        }    }

An abstract class can contain abstract and non-abstract methods, and when a class inherits from an abstract class, the derived class must implement all of the base class abstract methods.

An abstract method is a method that does not have a method body.

Abstract classMyabs { Public voidNonabmethod () {Console.WriteLine ("non-abstract Method"); }         Public Abstract voidAbmethod ();//An abstract method    }    classMyclass:myabs//must implement base class abstract methods    {         Public Override voidAbmethod () {Console.WriteLine ("Abstarct Method"); }    }    classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc.            Nonabmethod (); Mc.        Abmethod (); }    }

But by declaring that the derived class is also abstract, we can avoid the implementation of all or a particular virtual method, which is part of the implementation of the abstract class.

Abstract classMyabs { Public Abstract voidAbMethod1 ();  Public Abstract voidAbMethod2 (); }    //Not necessary to implement all abstract methods//partial implementation is possible    Abstract classMyclass1:myabs { Public Override voidAbMethod1 () {Console.WriteLine ("abstarct Method #1"); }    }    classMyclass:myclass1 { Public Override voidAbMethod2 () {Console.WriteLine ("abstarct Method #2"); }    }    classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc.            AbMethod1 (); Mc.        ABMETHOD2 (); }    }

In C #, an abstract class can inherit another non-abstract class, in addition, the method of inheriting the base class, adding a new abstract and non-abstract method is feasible.

classMyClass1//Non-abstract class    {         Public voidMethod1 () {Console.WriteLine ("Method of a non-abstract class"); }    }    Abstract classMyabs:myclass1//Inherits from an Non-abstract class    {         Public Abstract voidAbMethod1 (); }    classMyclass:myabs//must implement base class abstract methods    {         Public Override voidAbMethod1 () {Console.WriteLine ("abstarct method #1 of MyClass"); }    }    classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc.            Method1 (); Mc.        AbMethod1 (); }    }

An abstract class can also be implemented from an interface, in which case we must provide the method body for all the methods that come from the interface

InterfaceIInterface {voidMethod1 (); }    Abstract classMyabs:iinterface { Public voidMethod1 () {Console.WriteLine ("Method implemented from the IInterface"); }    }    classMyclass:myabs//must implement base class abstract method    {    }    classmyclient { Public Static voidMain () {MyClass MC=NewMyClass (); Mc.        Method1 (); }    }

We cannot use the keyword abstract with sealed in C # because a sealed class cannot be abstracted .

Abstract classMyabs { Public Abstract voidAbMethod1 ();  Public Abstract voidAbMethod2 (); }    classMyclass1:myabs { Public Override voidAbMethod1 () {Console.WriteLine ("abstarct method #1 of MyClass1"); }         Public Override voidAbMethod2 () {Console.WriteLine ("abstarct method #2 of MyClass1"); }    }    classmyclient { Public Static voidMain () {myabs ma1=NewMyClass1 ();//polymorphismMA1.            AbMethod1 (); Ma1.        ABMETHOD2 (); }    }

Abstract methods have the following characteristics:

1. An abstract method can be thought of as a virtual function.

2. The declaration of an abstract method can only be in an abstract class.

3. Because the abstract method declaration only provides a non-implementation way, there is no method body

4. Method body implementation of the Overwrite method is provided, the overridden method is a member of a non-abstract class.

5. The behavior of abstract attributes is similar to abstract methods, except for different declarative forms.

6. Using abstract in a static property is an error.

* An abstract property can be implemented using override with a derived class.

For abstract classes:

An abstract class must provide implementations for all interface members
An abstract class for implementing an interface might arrange an interface method on an abstract method. For example

Interface I {    void  M ();} Abstract class c:i {    publicabstractvoid  M ();}

Abstract classes have the following characteristics:

1. Abstract classes cannot be instantiated.

2. Abstract classes can contain abstract methods and accessors

3. It is not possible to modify an abstract class with a seal (sealed), which means that the class cannot be inherited, which violates the principle that the abstract class is inherited.

4. A non-abstract class derived from an abstract class must include all the inherited abstract methods and accessors of the implementation

5. The use of the abstract keyword in methods and properties implies that the implementation contains them.






C # abstract class abstraction

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.