The difference between an interface and an abstract class (classic)

Source: Internet
Author: User

Go to

The difference between an interface and an abstract class:

1The interface is the core, which defines what to do, contains many methods, but does not define how these methods should be done。
2If many classes implement an interface, each will implement those methods in code
3if the implementation of some classes has something in common, then an abstract class can be abstracted, allowing the abstract class to implement the common code of the interface, while the individual methods are implemented by the subclasses.。

SoAbstract classes are designed to simplify the implementation of interfaces,not only does he provide a common method of implementation, so you can quickly develop, but also allow your class to fully implement all the methods, without the problem of tight coupling。

The application is simple.
1Defining interfaces First
2 ifIf there are multiple interfaces that have existing common parts, use abstract classes, and then integrate it.

the difference between an interface and an abstract class--I'm sure you'll never confuse it.

I think, for you to use the object-oriented programming language programmer, "interface" this noun must not be unfamiliar, but I do not know if you have such doubts: what is the purpose of the interface? What is the difference between it and an abstract class? Can you use abstract classes instead of interfaces? And, as programmers, you must often hear the phrase "interface-oriented programming," What does it mean? What is the connotation of the mind? What is the relationship with object-oriented programming? This article will answer these questions.

1. What is the relationship between interface-oriented programming and object-oriented programming?

First of allinterface-oriented programming and object-oriented programming are not lateral, it is not an independent programming idea that is more advanced than object-oriented programming, but is attached to the object-oriented ideology, which belongs to its part. Or, it is one of the essence of thought in object-oriented programming system.。

2. The nature of the interface

Interfaceon the surface is a collection of several method definitions without the principal code, there is a unique name that can be implemented by a class or other interface (or, as can be said, inherited). It may look like this in form:

Interface InterfaceName {     void  Method1 ();      void METHOD2 (int  para1);      void

So what is the nature of the interface? Or what the meaning of the interface exists. I think it can be considered from the following two points of view:

1)An interface is a set of rules that specify a set of rules that a class or interface that implements this interface must have. Embodies the nature "if you are ... You must be able to ... "Concept。

For example, in nature, people can eat, that is, "If you are human, you must be able to eat". So the simulation to the computer program, there should be a IPerson (the custom interface name from the "I") interface, and there is a method called Eat (), and then we stipulate that each class that represents "human", must implement the IPerson interface, which simulates the nature "If you are human, you must be able to eat" This rule.

From here, I think you can see a little bit of object-oriented of the core of object-oriented thinking is to simulate the real world and abstract the real world into classes, and the whole program relies on each kind of instance to communicate with each other and to cooperate with each other to complete the system function, which is very accord with the real world running condition and the essence of object oriented thought.

2)an interface is an abstract representation of a similar thing on a certain granularity view. Note Here I emphasize on a certain granularity view , because the concept of "homogeneous thing" is relative, it is different because the granularity view is different。

For example, in my eyes, I am a person, and a pig there is an essential difference, I can accept my classmates and I are similar this statement, but can not accept me and a pig is the same. However, if in the eyes of a zoologist, I and pig should be the same, because we are animals, he can think that "man" and "pig" have achieved ianimal this interface, and he in the study of animal behavior, I and pig will not be treated separately, but from the "animal" This larger grain size study, But he would think I was fundamentally different from a tree.

Now for a geneticist, the situation is different, because the organism can inherit, so in his eyes, I not only with the pig no difference, and a mosquito, a bacterium, a tree, a mushroom and even a SARS virus is no different, Because he would think that we all achieved idescendable this interface (note: Descend VI. Heredity), that is, we are all heritable things, he will not study us separately, but will all creatures as the same kind of research, in his eyes there is no human and virus, only the genetic material and non-hereditary material. But at least, I have a difference with a stone.

But the unfortunate thing happened, some day, there appeared a great man, his name is Lenin, he was familiar with Max, Engels of dialectical materialism thought after the masterpiece, quite experience, so he made a famous definition: the so-called material, is to be reflected in the consciousness of the objective reality. At this point, I and a stone, a trace of air, an idiom and transmission of mobile phone signal electromagnetic field has no difference, because in Lenin's eyes, we are can be reflected in the consciousness of the objective reality. If Lenin was a programmer, he would say: the so-called matter is all the instances that are generated by all classes that simultaneously implement the "Ireflectabe" and "Iesse" two interfaces. (Note: Reflect v. reflect esse N. Objective reality)

You might think that my example above is a nonsense, but that's what the interface is all about. Object-oriented thinking and one of the core is called polymorphism, what is polymorphism? To put it bluntly is to treat the same things indiscriminately at a certain granularity view level. And the reason to do this is because there is an interface exists. Like the geneticist, he knew that all living creatures had idescendable interfaces, and that, as long as they were creatures, there must be descend (), so that he could study together, rather than study each creature and end up exhausted.

Maybe I can't give you a visual impression of the nature and function of the interface. Then in the following example and in the analysis of several design patterns, you will be more intuitive to experience the connotation of the interface.

3. Overview of interface-oriented programming

Through the above, I think we have an understanding of the interface and the concept of interface, then what is interface-oriented programming? My personal definition is:in system analysis and architecture, hierarchy and dependency are distinguished, each level is not directly to its upper layer to provide services (that is, not directly instantiated in the upper layer), but by defining a set of interfaces, only the upper layer exposes its interface function, the upper layer for the lower layer is only the interface dependency, and not rely on the specific class.

The benefits of doing so are obvious,First, there is a great benefit to system flexibility. When the lower layer needs to change, as long as the interface and interface functions are not changed, the upper layer does not have to make any changes. You can even replace the entire lower layer without altering the upper code ., just like we replace a WD 60G hard drive with a Seagate 160G hard drive, the rest of the computer does not have to make any changes, but the original hard drive to unplug, the new hard drive plug on the line, because the other parts of the computer is not dependent on the specific hard disk, and only rely on an IDE interface, as long as the hard disk implementation of this can be replaced. From this point of view, the interface in the program is very similar to the interface in reality, so I always think that the word interface (interface) is really in the likeness!

Another advantage of using the interface is that different parts or levels of developers can work in parallel, like making a hard disk without the CPU, and do not wait for the display, as long as the interface is consistent, reasonable design, can be developed in parallel,thereby improving efficiency.

This article first came here. Finally, I want to say one more word:the essence of object-oriented is to simulate reality, which is also the soul of my article. Therefore, more from the reality of thinking about object-oriented things, to improve the system analysis and design ability has great spleen benefits.

In the next article, I'll use an example to illustrate the basic methods of interface programming.

And the third, I will analyze some of the classic design patterns of interface-oriented programming ideas, and analysis. The interface-oriented idea in the net layered architecture.

Supplement to this article:

1. About "interface" in "interface-oriented programming" and "interfaces" in specific object-oriented languages two words

See thereA friend proposed that "interface" in "interface-oriented programming" should be larger than the interface in a simple programming language. I think it makes sense.。 I did not write that very well here. I think that "interface" in object-oriented language refers to a specific code structure, such as the interface defined in C # with the interface keyword. "Interfaces" in "interface-oriented programming" can be described as a structural component that hides concrete underlying classes and implements polymorphism from a software architecture perspective, from a more abstract level .。 In this sense, if an abstract class is defined and the purpose is to achieve polymorphism, then I think it is reasonable to refer to this abstract class as an "interface". But is it unreasonable to use abstract class to realize polymorphism? Discussed in the second article below.

Generally speaking,I think the concept of two "interfaces" is both different and interconnected.。The interface in "interface-oriented programming" is an architectural component of the thought-level for polymorphism, software flexibility, and maintainability, and the "interface" in a specific language is the means by which the parts of the idea are implemented into code.

2. About abstract classes and interfaces

If you look at the specific code alone,it's easy to blur the two concepts and even think the interface is superfluous., because from a specific function alone,In addition to multiple inheritance (in C#,java),abstract classes seem to completely replace interfaces。 Butdoes the presence of an interface exist to achieve multiple inheritance? Of course not .。 I thinkthe difference between an abstract class and an interface is the use of motivation. Abstract classes are used for reuse of code, and the motivation for using interfaces is to achieve polymorphism。 SoIf you're hesitant about where to use interfaces or abstract classes, think about what your motives are.。

See a friend to IPerson this interface query, my personal understanding is, IPerson this interface should not be defined, the key to see the specific application is how a situation. If our project has women and man, all inherit person, and women and man most methods are the same, only one method DOSOMETHINGINWC () different (the example is vulgar, you forgive me), Then of course it is reasonable to define a Abstractperson abstract class, because it can include all other methods, the subclass only defines DOSOMETHINGINWC (), and greatly reduces the amount of duplicated code.

However, if the women and man two classes in our program basically do not have common code, and there is a personhandle class that needs to instantiate them and do not want to know that they are male or female, and simply treat them as human beings and realize polymorphism, then it is necessary to define interfaces.

All in all , the difference between an interface and an abstract class lies primarily in the motivation used, not in itself. And whether a thing should be defined as an abstract class or an interface, depending on the context of the specific environment。

Furthermore, I thinkAnother difference between an interface and an abstract class is that it should be a general and special relationship between an abstract class and its subclasses,and the interface is just a set of rules that its subclasses should implement。 Of courseSometimes there may be general and special relationships, but the purpose of our use of interfaces is not hereFor example, vehicles, airplanes, and ships defined as sub-categories are acceptable because cars, airplanes, and ships are a special kind of transportation. Another example is the IComparable interface, which simply says that the class that implements this interface must be able to be compared, which is a rule. If the car class implements the IComparable, just saying that there is a way in our car to compare two car instances, it may be more expensive than which car, it may be bigger than which car, it does not matter, but we can not say that "the car is a special can be compared", which is not in grammar

The difference between an interface and an abstract class (classic)

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: 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.