Learning logs-abstract class and interface

Source: Internet
Author: User

Learn to remember a little bit so that you do not forget it ..

 

I read many articles written by my predecessors about abstract class and interface, and I tried to sort them out.

 

Abstract class:

1. abstract classes cannot be instantiated.

2. abstract classes can have constructor methods and can be called in Their Derived classes.

3. abstract methods in abstract classes (without method bodies) must be overwritten in the derived classes. Non-Abstract METHODS (with method bodies) can not be overwritten.

4. abstract classes can contain fields, attributes, methods, constructors, and other Members.
5. abstract classes can only satisfy single inheritance for interfaces.

 

 

Code

Abstract class Myabstract
{
Public Myabstract () {Console. WriteLine ("Abstract class constructors ");}
Public abstract void abstractMethod ();
Public void Method ()
{
Console. WriteLine ("Myabstract Method ");
}
}

 

 

Interface:

1. The interface cannot be instantiated.

2. The interface cannot have constructor methods.

3. the method in the interface does not need a method body. You must override the method in the derived class.

4. The interface cannot have fields.
5. There is no modifier for the interface method, and its meaning is similar to public.

6. The interface can meet the requirements of "Multi-inheritance ".

 

 

Interface MyInterface
{
Void interfaceMethod ();
}

 

 

Class:

 

Code

Class MyClass: Myabstract, MyInterface
{
Public MyClass ()
: Base ()
{
}

Public override void abstractMethod ()
{
Console. WriteLine ("MyClass Override Abstract Method ");
}

Public void interfaceMethod ()
{
Console. WriteLine ("MyClass Override Interface Method ");
}
}

 

 

Main Method:

 

Code

Class Program
{
Static void Main (string [] args)
{
MyClass myclass = new MyClass ();
Myclass. abstractMethod ();
Myclass. interfaceMethod ();
Myclass. Method ();
}
}

 

 

Output:

Abstract Class Constructors

MyClass Override Abstract Method

MyClass Override Interface Method

Myabstract Method

 

 

 

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.