Abstract class Interface

Source: Internet
Author: User

I. abstract classes/abstract methods

1. abstract methods are modified with abstract.

2. Abstract METHODS cannot have method bodies (nor can there be large arc ).

Eg:

          }

3. abstract methods must be in abstract classes.

4. abstract classes cannot be instantiated. Abstract members have no method bodies.

5. The subclass must override the abstract method of the parent class.

6. the abstract method of the parent class cannot be called through the base keyword in the subclass.

7. The abstract method is "do not do", but defines such behavior, but the specific implementation is handed over to the subclass.

8. abstract classes can have non-Abstract members. To inherit from them.

9. The abstract class can have virtual methods.

10. Usage of abstract classes: 1) The subclass must override the method of the parent class. 2) There is no need to instantiate the parent class.

11. abstract classes are inherited for polymorphism.

12. Abstract members cannot be private, because sub-classes cannot be rewritten without methods.

Eg1:

Console. WriteLine (Main (Computer c = UDisk =}View Code

Eg2:

Console. WriteLine (Console. WriteLine (Main (Console. WriteLine (str = NoteBook book = NoteBook GetANoteBook (type = returu (type =}View Code


Ii. ToString/Equals Method

Eg:

Name {; Main (Person p = Person () {Name = Person p1 = Person () {Name = B = p. equals (p1); Console. writeLine (p); Console. writeLine (B); str0 = str1 = B = str0.Equals (str1); Console. writeLine (B );}View Code

Iii. Interfaces

1. Define an interface with an interface. It is a special abstract class and cannot be instantiated.

2. The members do not need to add abstract or access modifiers. The default value is public abstract.

3. interfaces can only define attributes, methods, indexers, and events.

4. The class that implements the interface must implement all the members of the interface.

5. When implementing interface members, the implementation class does not need to write the override keyword.

6. interfaces are purely implemented to standardize classes.

7. You can find the parent class and want to inherit from the Child class to use the abstract class. Multiple classes have the same method, but no parent class can be found. You can define the method in the interface to implement these classes.

8. A class can only inherit one parent class, but multiple interfaces can be implemented. The parent class should be written in the first position, which solves the single-root problem of the class to a certain extent.

9. interfaces can inherit interfaces.

10. The member in the interface can be implemented as an abstract or virtual method, so that the sub-class of the implementation class can be implemented.

11. The display implementation interface is used to solve the problem of Method Name Conflict. The display implementation interface method is private, so it cannot be called through the object variables.

12. display the implementation interface. The implementation method of this interface can only be called through interface variables.

Eg:

{Name =}View Code

 

Related Article

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.