[C #] C # abstract classes and their methods

Source: Internet
Author: User
Tags define abstract

I. Interpretation 1

Use the keyword abstract in C # To define abstract classes and abstract methods.

Classes that cannot be initialized are called abstract classes. They only provide partial implementation, but the other class can inherit it and create them.
.

"An abstract class that contains one or more pure virtual functions cannot be instantiated.
An abstract class can only be used by interfaces and as the base classes of other classes. "-C ++ programming language by stroustrup chapter13.2

Abstract classes can be used for classes, methods, attributes, indexers, and events. Abstract indicates that the class tends to be used as the base class of other classes in a class declaration.
A member is marked as abstract or included in an abstract class, which must be implemented by its derived class.

 

Abstract class shapesclass
{
Abstract Public int area ();
}
Class square: shapesclass
{
Int X, Y;
// Not providing an Area Method Results
// In a compile-time error.
Public override int area ()
{
Return x * Y;
}
}

For example, an abstract class containing non-Abstract METHODS:

 

Abstract class myabs
{
Public void nonabmethod ()
{
Console. writeline ("non-abstract method ");
}
}
Class myclass: myabs
{
}
Class myclient
{
Public static void main ()
{
// Myabs MB = new myabs (); // not possible to create an instance
Myclass MC = new myclass ();
MC. nonabmethod (); // displays 'non-Abstract Method'
}
}

An abstract class can contain abstract and non-Abstract METHODS. When a class inherits from an abstract class, this derived class must implement all
.

An abstract method is a method without a method body.

Abstract class myabs
{
Public void nonabmethod ()
{
Console. writeline ("non-abstract method ");
}
Public abstract void abmethod (); // an abstract Method
}
Class myclass: myabs // must implement base class Abstract METHODS
{
Public override void abmethod ()
{
Console. writeline ("Abstarct method ");
}
}
Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. nonabmethod ();
MC. abmethod ();
}
}

However, by declaring a derived class as an abstraction, we can avoid the implementation of all or specific virtual methods,
This is part of the abstract class implementation.

Abstract class myabs
{
Public abstract void abmethod1 ();
Public abstract void abmethod2 ();
}
// Not necessary to implement all abstract methods
// Partial implementation is possible
Abstract class myclass1: myabs
{
Public override void abmethod1 ()
{
Console. writeline ("Abstarct method #1 ");
}
}
Class myclass: myclass1
{
Public override void abmethod2 ()
{
Console. writeline ("Abstarct method #2 ");
}
}
Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. abmethod1 ();
MC. abmethod2 ();
}
}

In C #, an abstract class can inherit from another non-abstract class. In addition, it inherits the methods of the base class and adds a new
Abstract and non-abstract methods are feasible.

Class myclass1 // non-abstract class
{
Public void Method1 ()
{
Console. writeline ("method of a non-abstract class ");
}
}
Abstract class myabs: myclass1 // inherits from an non-abstract class
{
Public abstract void abmethod1 ();
}
Class myclass: myabs // must implement base class Abstract METHODS
{
Public override void abmethod1 ()
{
Console. writeline ("Abstarct method #1 of myclass ");
}
}
Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. Method1 ();
MC. abmethod1 ();
}
}

An abstract class can also be implemented from an interface. In this case, we must provide the method body for all methods. These methods are from interfaces.

Interface iinterface
{
Void Method1 ();
}
Abstract class myabs: iinterface
{
Public void Method1 ()
{
Console. writeline ("method implemented from the iinterface ");
}
}
Class myclass: myabs // must implement base class abstract Method
{
}
Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. Method1 ();
}
}

We cannot use abstract keywords and sealed together in C #, because a sealed class cannot be abstracted.

Abstract class myabs
{
Public abstract void abmethod1 ();
Public abstract void abmethod2 ();
}
Class myclass1: myabs
{
Public override void abmethod1 ()
{
Console. writeline ("Abstarct method #1 of myclass1 ");
}
Public override void abmethod2 ()
{
Console. writeline ("Abstarct method #2 of myclass1 ");
}
}
Class myclient
{
Public static void main ()
{
Myabs ma1 = new myclass1 (); // Polymorphism
Ma1.abmethod1 ();
Ma1.abmethod2 ();
}
}

Abstract methods have the following features:

1. An abstract method can be considered as a virtual function.

2. The declaration of abstract methods can only be in abstract classes.

3. Because the abstract method declaration only provides one unimplemented method, there is no method body

4. The method body implementation is provided by the overwriting method. The overwriting method is a member of a non-abstract class.

5. The behavior of abstract attributes is similar to that of abstract methods, except for different declarations.

6. using abstract in a static attribute is an error.

* An abstract attribute can be implemented by using override in a derived class.

For abstract classes:

An abstract class must be implemented for all interface members.
An abstract class used to implement interfaces may arrange interface methods to abstract methods. For example

Interface I
{
Void M ();
}
Abstract class C: I
{
Public abstract void M ();
}

Abstract classes have the following features:

1. abstract classes cannot be instantiated.

2. abstract classes can contain abstract methods and accessors

3. the abstract class cannot be modified by sealed, which means that the class cannot be inherited, which violates the principle of being inherited.

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

5. Using abstract keywords in methods and attributes means that they are included.

Ii. Interpretation 2

// Abstract method: it only contains the method definition, but does not have a specific implementation method. It must be implemented by its subclass or subclass. // Static methods cannot be marked as override, virtual, or abstract, that is, static methods must be specific/abstract classes: Classes containing one or more abstract methods are called abstract classes, before the class name, you must add the "abstract" keyword // The abstract class can contain non-Abstract METHODS // The abstract class cannot be instantiated because it contains methods without specific implementation, classes that can be instantiated must not be abstract classes, and do not contain abstract methods that are not specifically implemented. // After the subclass inherits the abstract parent class, you can use the override keyword to overwrite the abstract methods in the parent class and perform specific implementation. You can also leave the abstract method to future generations for implementation. At this time, the subclass is still an abstract class, and the abstract method that must be declared as abstract/inheritance cannot be hidden/hidden: create a method in the subclass with the same signature as the method in the parent class (same method name, same parameter list-parameter type and order) methods (with the keyword "virtual" or "Override") can be implemented, but we recommend that you use the "new" keyword to explicitly hide it. // Only the "Override" keyword can be used to overwrite (override) Methods marked as "virtual", "abstract", or "Override" in the parent class, and the method marked as override in the subclass, it must also be the method marked as "virtual", "abstract", or "Override" in the parent class. // Override: the override keyword must be used. methods that can be overwritten include methods marked as abstract, virtual, and override. // hide: use the new keyword, you do not need to use keywords. Hidden methods include general methods and methods marked as virtual or override. // overload ): no special keywords are required. // static methods can be hidden or overloaded.
Iii. Explanation 3
1. Declare an abstract method using the abstract keyword.
2. A class can contain one or more abstract methods.
3. Non-abstract methods can exist in abstract classes.
4. abstract classes cannot be directly instantiated.
5. the abstract class is implemented using ":" (colon), and the abstract method is implemented using the override keyword.
6. the abstract class can be inherited by the abstract class, and the result is still the abstract class.
7. After an abstract method is implemented, the modifier cannot be changed.

Using system;

Namespace effecact_for
{

/// <Summary>
/// Abstract class ABC
/// </Summary>
Abstract Class ABC
{
Public abstract void afunc (); // declares the abstract method, but does not implement

Public void bfunc ()
{
Console. writeline ("non-abstract method! ");
}
}

/// <Summary>
/// The derived class derv of the abstract class ABC
/// </Summary>
Class derv: ABC
{// This class must implement the abstract method of the base class, otherwise it must be declared as abstract
Public override void afunc ()
{// You can use the override keyword to implement an abstract method in a derived class! The override method declared through override becomes the override base class method.
Console. writeline ("Implementing abstract methods ");
}
}
Public class test {
Public static void main (string [] ARGs)
{
Derv d = new derv ();
D. afunc ();
D. bfunc ();
}
}

}

-----------------------------------------

Abstract classes described in abstract cannot be instantiated. They can only be inherited. Example: // abstract class drawing class {public void drawing {}}// inheritance class painting triangle class: drawing class {public void drawing {// Method for triangle drawing} // inheritance class circle drawing class: drawing class {public void drawing {// Method for circle drawing} OK! Paint1 = new paint1 (); paint1. drawing (); // paint2 = new paint2 (); paint2. drawing (); // circle

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.