Abstract class and method in C !!!

Source: Internet
Author: User
Abstract class in C # cannot be instantiated. It only provides inherited interfaces of other classes.

Using system;
Abstract class myabs
{
Public void nonabmethod ()
{
Console. writeline ("non-abstract method ");
}
}

Class myclass: myabs
{
}

Class myclient
{
Public static void main ()
{
// Myabs MB = new myabs (); // cannot be instantiated

Myclass MC = new myclass ();
MC. nonabmethod ();
}
}

An abstract class can contain abstract methods or instantiation methods, but an inherited class (non-Abstract) must implement abstract methods.
Using system;

Abstract class myabs
{
Public void nonabmethod ()
{
Console. writeline ("non-abstract method ");
}
Public abstract void abmethod (); // abstract method, only declaration, not implemented
}

Class myclass: myabs // The abstract method must be implemented
{
Public override void abmethod ()
{
Console. writeline ("Abstarct method ");
}
}

Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. nonabmethod ();
MC. abmethod ();
}
}

Of course, the inheritance class can also be abstract.

Using system;

Abstract class myabs
{
Public abstract void abmethod1 ();
Public abstract void abmethod2 ();
}

// The abstract inheritance class does not have to implement all the abstract methods, and can be partially implemented.

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 ();
}
}

Abstract classes can inherit from non-abstract classes.

Using system;

Class myclass1
{
Public void Method1 ()
{
Console. writeline ("method of a non-abstract class ");
}
}

Abstract class myabs: myclass1
{
Public abstract void abmethod1 ();
}

Class myclass: myabs // The abstract method must be implemented for the instance class
{
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 ();

}
}

Abstract classes can implement interfaces

Using system;

Interface iinterface
{
Void Method1 ();
}

Abstract class myabs: iinterface
{
Public void Method1 ()
{
Console. writeline ("method implemented from the iinterface ");
}
}

Class myclass: myabs
{

}

Class myclient
{
Public static void main ()
{
Myclass MC = new myclass ();
MC. Method1 ();
}
}

Note that the abstract class cannot be declared as sealed. The two semantics conflict. Abstract methods do not need to be declared as virtual because they are implicitly virtual by default!

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.