Abstract class and Interface essence

Source: Internet
Author: User

Abstract class-related

abstract class: also called abstract base class: A generic class that is marked with the abstract keyword when defined. He can contain features that are contained in a generic class, such as fields, properties, methods, and a special method called an abstract method (a method that is not implemented, and a class derived from that class must implement the method), and cannot be instantiated, mainly in terms of the definition and partial implementation of the class.

Let's take a look at the abstract and abstract methods where we need to be aware of it:

1, abstract classes need to be marked with the abstract keyword

2, abstract methods cannot be implemented in any way (not even {})

3, an abstract member must be contained in an abstract class (subordinate to).

4, because the abstract member does not have any implementations, the subclass must override the abstract member.

5. Abstract classes cannot be instantiated, so the existence of abstract classes is to allow subclasses to inherit.

6, an abstract class can include either abstract members or members with code implementations. ,

7, abstract methods cannot be modified with static.

After looking at 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 an interface type, which can contain an instance/static method, and any class that implements the interface must implement these methods as defined by the interface.

Let's take a look at where to use the interface:

1, interface is a specification, in order to achieve polymorphism and exist

2, the interface cannot be instantiated

3, the member in the interface cannot be added "access modifier", the member access modifier in the interface is public and cannot be modified (default is public)

4, the members of the interface cannot have any implementations ("Light says not done", but define a set of non-implemented members)

5, the interface can only have methods, properties, primers and events, cannot have a "field"

6, interface and interface can inherit, and can inherit multiple

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

8, a class can inherit a class at the same time and implement multiple interfaces, if a class inherits the parent class A and implements the interface IA, then syntactically a must be written in front of the IA. Class test:a,ia{}, because classes are single-inheritance.

9, when an abstract class implements an interface, if you do not want to implement the member in the interface, you can implement the member as abstract

10, display implementation interface, can only be called through an interface variable (because the member is private after displaying the implementation interface)

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

First: interface defines the public behavior of the class, and the abstract class defines the public implementation of the class

Second: A class can inherit from only one class (abstract class), but it is possible to implement multiple interfaces at the same time

Third: An interface cannot have an implementation, an abstract class can have neither an implemented member nor an implemented member

IV: Methods that are not implemented in an interface must be implemented in subclasses, and members that are not implemented in an abstract class must be overridden in a subclass

Five: Good interface definition should be functional, not multi-functional, otherwise it will cause interface pollution. If a class just implements a function in this interface, and has to implement other methods in the interface, it is called interface pollution.

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

When do I use an abstract class and when do I use an interface?

First, we emphasize that abstract classes are primarily used for closely related objects, and that interfaces are suitable for providing common functionality for unrelated classes.

For example: Airplanes can fly, birds can fly, they all inherit the same interface "Fly", but "Boeing 747" belongs to the airplane abstract class, the wild goose belongs to the bird abstract class.

Or the following analogy:

Like iron doors are doors (abstract class), you want a door I can't give (can't instantiate), but I can give you a concrete iron gate or wooden door (polymorphic); and can only be the door, you can not say it is a window (single inheritance), a door can have a lock (interface) can also have a doorbell (multi-implementation). The door (abstract class) defines what you are, and the interface (lock) specifies what you can do (an interface is best to do only one thing, and you cannot ask for a lock to make a sound (interface pollution)).

Finally, a simple table is used to compare the two:

Abstract class and Interface essence

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.