C # abstract classes and abstract methods

Source: Internet
Author: User

If a class is not associated with a specific thing, but only expresses an abstract concept, it is only used as a base class of its derived class. Such a class is an abstract class, when declaring a method in an abstract class, adding abstract is an abstract method.

1. abstract class overview and Declaration

Main differences between abstract classes and non-abstract classes:

· Abstract classes cannot be directly instantiated.

· Abstract classes can contain abstract members, but not abstract classes.

· Abstract classes cannot be sealed

For example

Public abstract class oneClass

{

Public int I;

Public void denylau ()

{

}

}

2. Overview and declaration of abstract methods

Note the following when declaring an abstract method: · An abstract method must be declared in an abstract class. When declaring an abstract method, virtual, static, and private modifiers cannot be used.

Abstract methods are not implemented in abstract classes.

For example:

Public abstract class myTestClass ()

{

Public abstract void method ();

}

3. Use of abstract classes and abstract methods

Here is an example!

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace _
{
Public abstract class myClass
{
Private string id = "";
Private string name = "";
/// <Summary>
/// Number attributes and implementation
/// </Summary>
Public string ID
{
Get
{
Return id;
}
Set
{
Id = value;
}
}
/// <Summary>
/// Name attributes and implementation
/// </Summary>
Public string Name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
/// <Summary>
/// Abstract method used to output information
/// </Summary>
Public abstract void ShowInfo ();
}
Public class DriveClass: myClass // inherits the abstract class
{
/// <Summary>
/// Override the output information in the abstract class
/// </Summary>
Public override void ShowInfo ()
{
Console. WriteLine (ID + "" + Name );
}
}
Class Program
{
Static void Main (string [] args)
{
DriveClass driveclass = new DriveClass (); // instantiate a derived class
MyClass myclass = driveclass; // use a derived class object to instantiate an abstract class
Myclass. ID = "BH0001"; // use an abstract class object to access the numbering attribute in the abstract class
Myclass. Name = "TM"; // use an abstract class object to access the Name attribute in the abstract class
Myclass. ShowInfo (); // use an abstract class object to call the abstract method in the abstract class
}
}
}

In the above example, the abstract class is instantiated through the driveclass object of the derived class, and then the attributes and methods in the abstract class are accessed using the abstract class.

In the small example above, careful friends will find out, how is the abstract class similar to the interface described in the previous article? So what are the differences between abstract classes and interfaces?

OK. The following describes the differences between abstract classes and interfaces:

· Their Derived classes can only inherit one base class, that is, only one abstract class can be inherited, but multiple interfaces can be inherited.

· The implementation of members can be defined in abstract classes, but not in interfaces.

· Abstract classes include fields, constructor, destructor, static members, or constants. APIs are not allowed.

· The members in the abstract class can be private (as long as they are not abstract), protected, internal, or protected internal members, but the members in the interface must be public.

PS: abstract classes and interfaces are used for completely different purposes. Abstract classes are mainly used as the base classes of object series and share some main features, such as the common purpose and structure. Interfaces are mainly used for classes. These classes are different at the basic level, but some identical tasks can still be completed.

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.