On the connection and difference between interfaces and abstractions

Source: Internet
Author: User
Many times, when I interview others, I often ask, what is the difference between interfaces and abstraction? I think this can well examine the comprehensive object-oriented capabilities of candidates, I collected conclusions from some netizens on the Internet. Let's take a look.

In simple terms, the interface is public, and there cannot be private methods or variables in it. They are used by others. abstract classes can have private methods or private variables.

In addition, the interface must implement all the methods defined in the interface, and the methods to be used can be selectively rewritten to implement the abstract class. In general applications, the top level is the interface, then the abstract class implements the interface, and the class is implemented.

In addition, an interface can implement multi-inheritance, while a class can only inherit one super class, but it can implement multi-inheritance by inheriting multiple interfaces. The interface also has an identifier (there is no method in it, such as the remote interface) and Data Sharing (the variables inside are constants.
 
I think there are two main differences between interfaces and abstract classes. One is that there can be non-abstract methods in abstract classes. Although abstract classes still cannot be instantiated, their subclasses can, that is to say, these non-Abstract Functions can still be called with polymorphism, Which is why abstract classes are not completely replaced by interfaces. An interface is the opposite. However, an interface also has an incomparable advantage: abstract classes can inherit multiple interfaces, while abstract classes can inherit only one class, just like normal classes, therefore, the interface can solve the problem of multi-inheritance.

Abstract classes can only implement some methods, while interfaces must implement all their methods. abstract classes can have abstract methods or common methods. Methods in interfaces must be abstract methods; abstract class methods can have multiple access permissions. interface methods can only have public access permissions. abstract classes can only inherit one abstract class, while interface classes can implement multiple interfaces at the same time.

Generally speaking:
Interface = defines all the things to do, but you do nothing.
Abstract class = has done part of the work together, and the rest of the work is not done.
OK, this is the difference!


Another article


I. abstract class:
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.

Ii. interface:
The interface is of reference type. It is similar to a class and has three similarities with the abstract class:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );

In addition, interfaces have the following features:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).

Iii. Differences between abstract classes and interfaces:
1. class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. the interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field behind it to prove that it represents a class "I can do it... ". Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.
5. the abstract class cannot be sealed.
6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them as virtual.
7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.
8. abstract classes implement a principle in OOP that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.
9. 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 of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in Asp.net, the page class has server request and other attributes, but in fact they are all objects of a certain class. This object of the page class is used to call the methods and attributes of other classes. This is a very basic design principle.
11. If an abstract class implements an interface, you can map the methods in the interface to the abstract class as an abstract method without having to implement it. Instead, you can implement the methods in the subclass of the abstract class.

Iv. Use of abstract classes and interfaces:
1. If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions.
2. If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces.
3. If you want to design a large functional unit, use the abstract class. If you want to provide general implemented functions among all the implementations of the component, use the abstract class.
4. abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for irrelevant classes.

The following are some of the Image metaphors I have seen on the Internet. They are really good:
1. Planes fly and birds fly. They all inherit the same interface "fly". However, f22 belongs to the aircraft abstract class and pigeon belongs to the bird abstract class.
2. just like all doors (abstract class), I can't give you a door (I can't instantiate it), but I can give you a specific door or wooden door (polymorphism ); it can only be a door. You cannot say it is a window (single inheritance); a door can have a lock (Interface) or a doorbell (multiple implementations ). A door (abstract class) defines what you are and an interface (LOCK) specifies what you can do (one interface is best to do only one thing, you cannot require the lock to make sound (interface pollution )).

Another expert said:

The interface is mainly used to describe some functions of the system and how to call these functions.
Layer abstraction. It is mainly used for external display. At the same time, the interface is relatively stable and cannot be changed at will. Because you are one
For the outside world, your performance has changed. The interface is equivalent to a standard description and feeling for the system.
It is a bit like virtual machine specifications for virtual machines. For programmers, interfaces help you hide something,
This writes something hidden (how to implement it) and you don't need to pay attention to it.

Abstract class. You can also say the difference in syntax. In fact, abstract classes can partially implement interface functions.
To describe something to the outside world. Abstract classes are more like the skeleton of a system, which has some basic needs to be shared.
. And some methods that have been implemented. Think about it. If all of them use interfaces instead of abstract classes, then we
Writing sub-classes below requires a lot of code. These subclasses can be used to share code and attributes.
. So abstract classes are more of an internal thing.

It can be said that an interface is more abstract than an abstract class. The interface and abstract class follow different places. Of course
Logically, their differences are not so obvious. However, in terms of usage, they are still relatively large.
Difference.

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.