Learning of C # abstract classes and their methods

Source: Internet
Author: User
Tags define abstract

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

Classes that cannot be initialized are called abstract classes, and they provide only partial implementations, but another class can inherit it and create them
The instance.

"A class that contains one or more pure virtual functions is called an abstract class, and abstract classes cannot be instantiated, further
An abstract class can only be used by interfaces and as base classes of other classes. " -C + + programming Language by Stroustrup Chapter13.2

Abstract classes can be used for classes, methods, properties, indexers, and events, using abstract in a class declaration to indicate that the class tends to be a base class for other classes
Members are marked as abstract, or are included in an abstract class and must be implemented by their derived classes.

Abstract class Shapesclass
{
Abstract public int area ();
}
Class Square:shapesclass
{
int x, y;
Not providing a area method results
In a compile-time error.
public override int Area ()
{
return x * y;
}
}


For example, an abstract class that contains 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, and when a class inherits from an abstract class, the derived class must implement all
The base class abstract method.

An abstract method is a method that does not have 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 ();
}
}



But by declaring that the derived class is also abstract, we can avoid the implementation of all or specific virtual methods,
This is part of the implementation of the abstract class.

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 another non-abstract class, and, in addition, inherits the method of the base class, adding 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 which case we must provide the method body for all the methods that come from the interface

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 the keyword abstract with sealed 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 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
{
public abstract void 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.

Learning of C # abstract classes and their methods

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.