The examples in this article describe the Java interface and abstract class usage. Share to everyone for your reference, specific as follows:
Interface
1 because Java does not support multiple inheritance, there is an interface where a class can inherit only one parent class, but can implement multiple interfaces, and the interface itself can inherit multiple interfaces.
The member variables in the 2 interface are all public static final types by default. Initialization that must be displayed.
The methods inside the 3 interface are all public abstract types by default. An implicit declaration.
The 4 interface has no construction method and cannot be instantiated.
The 5 interface cannot implement another interface, but it can inherit multiple interfaces.
6 class If you implement an interface, you must implement all the abstract methods inside the interface, otherwise the class is defined as an abstract class.
Abstract class
1 if a class is declared abstract, this class cannot generate an object and can only be used by inheritance.
2 abstract methods must exist in the abstract class.
3 abstract classes can have general variables and general methods.
4 subclasses inherit an abstract class must implement the abstract method, unless the subclass is an abstract class.
private void Print () {}; This statement represents a null implementation of the method.
abstract void print (); This statement represents the abstraction of the method, no implementation.
The difference between an interface and an abstract class
1 interfaces can contain only abstract methods, and abstract classes may contain common methods.
The 2 interface can only define static constant properties, which can either define normal properties or define static constants properties.
The 3 interface does not contain a constructor method, and an abstract class can contain a constructor method.
An abstract class cannot be instantiated, but does not mean that it cannot have constructors, an abstract class can have constructors, and an inheritance class extension
The 1 interface is the core that defines what to do, contains many methods, but does not define how these methods should be done.
2 If many classes implement an interface, then each will implement those methods in code
3 If the implementations of some classes have something in common, you can abstract an abstract class that enables the abstract class to implement common code for the interface, and those personalized methods are implemented by each subclass.
Therefore, the abstract class is to simplify the implementation of the interface, he not only provides the implementation of public methods, so that you can quickly develop, but also allows your class can fully implement all the methods, do not appear tight coupling problems.
It's a simple application situation.
1 Priority Definition Interface
2 If you have multiple interfaces that are already public, use an abstract class and then integrate it.
The difference between an interface and an abstract class--I'm sure you'll never get confused again.
I think, for everyone using object-oriented programming language programmers, the term "interface" is certainly not unfamiliar, but I do not know whether you have such a doubt: what is the use of interfaces? What is the difference between it and an abstract class? Can you use abstract classes instead of interfaces? And, as a programmer, you must often hear the phrase "interface-oriented programming," What does it mean? What is the ideological connotation? 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 all, interface programming and object-oriented programming is not a peer, it is not more advanced than object-oriented programming an independent programming idea, but attached to the object-oriented ideology, belong to its part. Or, it is one of the core ideas of object-oriented programming system.
2. The nature of the interface
interface, on the surface, is a collection of several method definitions without the principal code, with a unique name that can be implemented by a class or other interface (or, optionally, inherited). It may be in form as follows:
Interface InterfaceName
{
void Method1 ();
void Method2 (int para1);
void Method3 (String para2,string para3);
So what is the nature of the interface? Or what is the meaning of the interface. I think the following two perspectives can be considered:
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 ... The idea that you must be able to ... "
For example, in nature, people can eat, that is, "If you are human, you must be able to eat." So when you simulate a computer program, there should be a iperson (in a Habit, interface name by "I", 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 also see some object-oriented thinking things. One of the core of object-oriented thinking is to simulate the real world, to abstract things in the real world into classes, the whole program relies on each class instance to communicate with each other and complete the system function, which accords with the real world's operation condition and the essence of object-oriented thought.
2 Interface is an abstract representation of similar things in a certain granularity view . Notice here I emphasize that in a certain granularity view, because the concept of "similar things" is relative, it is different because of the granularity view.
For example, in my eyes, I am a person, and a pig is essential difference, I can accept my classmates and I are similar to this argument, but I can not accept that I and a pig is the same. But if, in the eyes of a zoologist, I and the pig should be of the same kind, because we are all animals, he can think that "man" and "pig" have realized the interface of IAnimal, while he is studying animal behavior, will not treat me and the pig separately, and will be from the "animal" This larger particle size study, But he would think that I was fundamentally different from a tree.
Now a geneticist, the situation is different, because biology can inherit, so in his eyes, I not only and pig no difference, and a mosquito, a bacterium, a tree, a mushroom and even a SARS virus are no different, Because he would think we all realized idescendable this interface (note: Descend VI. Heredity), that we are all heritable things, that he does not study us separately, and that all creatures are studied as one, and that in his eyes there are no people or viruses, only inheritable substances and not inheritable substances. But at least I have a difference with a stone.
But the unfortunate thing happened, one day, the Earth appeared a great person, his name is Lenin, he is familiar with Max, Engels thought of the dialectical materialism after a lot of experience, so he made a famous definition: the so-called material, is to be reflected by consciousness of the objective reality. At this point, I and a stone, a little air, an idiom and transmission cell phone signal electromagnetic field has no difference, because in Lenin's eyes, we can be reflected by the consciousness of the objective reality. If Lenin were a programmer, he would say, "a substance is an instance of all classes that implement both" Ireflectabe "and" Iesse "two interfaces. (Note: Reflect v. reflect esse N. Objective reality)
You might think that my example above is nonsense, but that's what the interface is all about. Object-oriented thought and one of the core is called polymorphism, what is polymorphism? To put it bluntly is to treat the same thing without discrimination on the level of a granular view. And the reason to dare to do so is because there is an interface. Like the geneticist, he understands that all creatures have idescendable interfaces, and that as long as they are creatures, there must be descend (), so that he can unify the study without studying each creature and eventually die.
It may not give you a visual impression of the nature and function of the interface. Then in the following examples and in the analysis of several design patterns, you will be more intuitive to experience the meaning of the interface.
3. Interface-Oriented Programming overview
Through the above, I think we have an understanding of the interface and the ideological content of the interface, then what is interface-oriented programming? My personal definition is: in system analysis and architecture, distinguish between hierarchy and dependencies, each level is not directly to the top of the service (that is, not directly instantiated in the upper layer), but by defining a set of interfaces, only to the top exposed its interface functionality, the upper layer for the lower level is only interface dependencies, and does not rely on specific classes.
The benefits of doing so are obvious, and first of all, it is good for system flexibility. When the lower level needs to change, as long as the interface and interface function unchanged, the upper layer does not make any changes. You can even replace the entire lower layer without changing the upper layer of code. Just like we replace a WD 60G hard disk with a Seagate 160G hard drive, the other parts of the computer do not have to make any changes, but the original hard drive, the new hard drive plug on the line, because the rest of the computer is not dependent on the specific hard disk, Instead of relying on only one IDE interface, as long as the hard drive implements this interface, it 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 a likeness!
Another advantage of using the interface is that different parts or levels of developers can work in parallel, just like the hard disk to build the CPU, and do not have to build a monitor, as long as the interface is consistent, reasonable design, can be developed in parallel, and thus improve efficiency.
This article first came here. Finally, I would like to say a word: the essence of object-oriented is to simulate reality, which can be said to be the soul of my article. Therefore, more from the reality of the object-oriented things, to improve the system analysis and design capacity of the spleen benefits.
In the next article, I'll use an example to illustrate the basic approach to interface programming.
And the third one, I will parse some of the classical design patterns in the interface-oriented programming ideas, and analysis. NET layered architecture in the interface-oriented thinking.
Add to this article:
Having read your reply carefully, I am very glad to discuss technical issues with you. Thanks for giving a certain friend, but also for the friends who made the comments and questions, which prompted me to think more deeply about something, hoping to make progress. Here I would like to add something to discuss some of the more focused answers.
1. About "interface" in "interface-oriented Programming" and "interface" two words in a specific object-oriented language
See a friend. The word "interface" in "interface-oriented programming" should be larger than the interface range in a simple programming language. After thinking, I felt very reasonable. What I'm writing here really doesn't make sense. I think that "interface" in object-oriented languages refers to a specific code structure, such as the interface defined by the interface keyword in C #. "Interface" in "interface-oriented programming" can be said to be a structural part that hides the concrete underlying class and implements polymorphism from the perspective of software architecture and from a more abstract level. In this sense, if you define an abstract class and the purpose is to implement polymorphism, then I think it is reasonable to refer to this abstract class as an "interface". But is it unreasonable to use abstract classes to realize polymorphism? Discussed in the second article below.
In a nutshell, I think the two "interface" concepts are different and interconnected. The interface in "interface-oriented programming" is a thought-level architecture component for implementing polymorphism, improving software flexibility and maintainability, while the "interface" in a specific language is the means by which parts of this thought are specifically implemented into the code.
2. About abstract classes and interfaces
This is a more heated question to see in the reply. I'm sorry I didn't think about the problem in the article. My personal understanding of this problem is as follows:
If you look at the specific code, it is easy to blur the two concepts, and even feel that the interface is redundant, because from the specific function, in addition to multiple inheritance (C#,java), the abstract class seems to completely replace the interface. However, does the interface exist to achieve multiple inheritance? Of course not. I think that the 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. So, if you are hesitant to use an interface or abstract class for a particular place, think about what your motives are.
See a friend of IPerson this interface query, my personal understanding is that IPerson this interface should not be defined, the key to see how the specific application of the situation. If we have women and man in our project, and we all inherit person, and most of women and man are the same, there is only one way DOSOMETHINGINWC () different (the example is vulgar, you forgive), Then of course it's reasonable to define a Abstractperson abstract class, because it can include all the other methods, and subclasses only define DOSOMETHINGINWC (), greatly reducing the amount of duplicate code.
However, if the women and the 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, but simply treat them as human beings and implement polymorphism, then it is necessary to define them as interfaces.
All in all, the difference between an interface and an abstract class lies primarily in the motivation to use, not in itself. And a thing that is defined as an abstract class or an interface depends on the context of the specific environment.
Furthermore, I think the other difference between an interface and an abstract class is that an abstract class and its subclasses should be a general and special relationship, whereas an interface is just a set of rules that its subclasses should implement. (Of course, sometimes there may be a general and special relationship, but we do not use the purpose of the interface here) such as the definition of transport vehicles, cars, airplanes, ships defined as subclasses, is acceptable, because cars, airplanes, ships are a special means of transport. Another example is the IComparable interface, which simply says that the class that implements this interface must be able to compare, which is a rule. If the car class implements IComparable, just say, there is a method in our car to compare the two car instances. It may be more expensive than a car, and it may be bigger than that, but it doesn't matter, but we can't say that "cars are a special kind of comparison", which doesn't make sense in grammar.
I hope this article will help you with Java programming.