Abstract classes and interfaces

Source: Internet
Author: User

Abstract class problems

Abstract class:Also called abstract base class: used for definitionAbstractA common class marked with a keyword. It can contain the characteristics of a general class, such as fields, attributes, and methods. It also contains a special method calledAbstract Method(The class derived from this class must implement the method without an implemented method) and cannot be instantiated. It is mainly used in class definition and partial implementation.

Next, let's take a look at the abstract classes and abstract methods to note when using them:

1. abstract classes must be marked with abstract keywords.

2. abstract methods cannot be implemented in any way ({} cannot be implemented)

3. Abstract members must be included in (affiliated to) abstract classes.

4. Since the abstract Members do not have any implementations, The subclass must rewrite the abstract members.

5. the abstract class cannot be instantiated. Therefore, the abstract class exists to inherit the subclass.

6. abstract classes can include both abstract members and abstract membersCodeImplementation member .,

7. abstract methods cannot be modified using static.

After reading abstract classes and abstract methods, let's look at the interface-related content.

Interface related:

Interface: An interface is a special abstract class that uses the interface keyword to mark the interface type. It can contain instances or static methods, any class that implements this interface must implement these methods according to the interface definition.

Next, let's take a look at the points to note when using interfaces:

1. An interface is a specification that exists for polymorphism.

2. The interface cannot be instantiated.

3. The "access modifier" cannot be added to the interface members. The access modifier of the interface members is public and cannot be modified (the default value is public)

4. The members in the interface cannot have any implementations ("simply do not do", but define a group of unimplemented members)

5. interfaces can only contain methods, attributes, cited devices, and events, and cannot contain "fields"

6. interfaces and interfaces can be inherited.

7. The subclass that implements the interface must implement all the members of the interface.

8. A class can inherit a class and implement multiple interfaces at the same time. If a class inherits both the parent class A and implements the interface IA, in terms of syntax, A must be written before IA. Class test: A, Ia {}, because the class is single-inherited.

9. When an abstract class implements an interface, if you do not want to implement the members in the interface, you can implement the members as abstract

 

10. display the implementation interface, which can only be called through the interface variable (because the Member is private after the implementation interface is displayed)

After reading the characteristics of abstract classes and interfaces, let's look at the differences between abstract classes and interfaces:

First, the interface defines the public behavior of the class, and the public Implementation of the abstract class definition class

Second, a class can only inherit from one class (abstract class), but multiple interfaces can be implemented at the same time.

Third: the interface cannot be implemented. The abstract class can contain neither unimplemented members nor implemented members.

Fourth, unimplemented methods in the interface must be implemented in the subclass, and unimplemented members in the abstract class must be rewritten in the subclass.

Fifth: A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function in this interface and has to implement other methods in the interface, it is called interface pollution.

After reading the differences between abstract classes and interfaces, let's look at the application scenarios of abstract classes and interfaces.

When to use abstract classes and interfaces?

First, we emphasize that abstract classes are mainly used for closely related objects, and interfaces are suitable for providing general functions for irrelevant classes.

For example, an airplane can fly and a bird can fly. They all inherit the same interface, "fly", but "Boeing 747" belongs to the abstract class of an airplane, and a goose belongs to the abstract class of a bird.

Finally, use a simple table to compare the two:

 

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.