Abstract classes and abstract methods

Source: Internet
Author: User

Overview: a class that is not associated with a specific thing only expresses an abstract concept and serves only as a base class of its derived class, provides public definitions of the base classes that can be shared by multiple Derived classes.

Declaration: access modifier abstract class name: base class or interface

{// Class member}

Note: Except abstract, class, and class names, other names can be omitted.

Different from non-abstract classes: abstract classes cannot be directly instantiated. abstract classes can contain abstract members. Non-abstract classes cannot contain abstract members. abstract classes cannot be sealed.

Abstract method: When an abstract class declares a method, it adds the abstract keyword. It only introduces a new method and does not provide the implementation of this method. Therefore, there is only one semicolon in the method body.

Must be declared in the abstract class. Virtual, static, and private modifiers cannot be used.

Note: When a non-abstract class is derived from an abstract class, you must overwrite the abstract method in the non-abstract class to provide specific implementation. The override keyword is used.

Instance:

Namespace _ abstract class and abstract method. CS

{

Public abstract class myclass

{

String id = "";

Public String ID // number attribute and Its Implementation

{Get {return ID;} set {id = value ;}}

String name = "";

Public string name // name attributes and their implementation

{Get {return name;} set {name = value ;}

}

Public abstract void showinfo (); // abstract method for outputting information}

Public class driveclass: myclass // inherits the abstract class

{

Public override void showinfo () // rewrite the output information in the abstract class

{Console. writeline (ID + "" + name );}

Class Program
{
Static void main (string [] ARGs)
{
Driveclass = new driveclass ();
Myclass = driveclass;
Myclass. ID = "bh0001 ";
Myclass. Name = "TM ";
Myclass. showinfo ();
Console. readkey ();
}

}

}

Abstract classes and interfaces:

Abstract classes and interfaces both contain members that can inherit from derived classes. They cannot be instantiated directly, but their variables can be declared.

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, which are different at the basic level,

However, you can perform some identical tasks.

Difference: A derived class can inherit only one base class, that is, only one abstract class, but can inherit any number of interfaces.

Abstract classes can define Member implementations, but interfaces cannot.

Abstract classes can contain fields, constructors, destructor, static members, or constants. interfaces are not allowed.

The members in the interface must be common, but the abstract class can be other

Abstract classes and abstract 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.