Subject: [150 points] discussion of why to use interfaces:? Zhaoqiubo (Bear Jeremy) credit value: 100 Quantity Forum: Java design patterns issue Points: 150 Views Replies: 45 Time: 2004-10-2014: 47:59 I have understood the connection to the interface, but multiple inherits have some questions. . .
Everyone knows that the method in the interface is not detailed. Just is a name. Although it can help us achieve multiple inheritance, it doesn't have a way, that is, there is no implementation method. Even if we achieve multiple inheritance, What is the use ?? Is it just to inherit a method? Is this meaningful? I still have to write the process of implementation. Even if you don't use the interface, you don't achieve multiple inheritance, then it is also written Procedure, but don't use the amount of code to reduce the interface !!
Do not understand .... Please advice. Best to give an example, explain how if there is an interface? If there is no interface? Why do you have an interface ....
Reply to: YZ790724 (think about) () Reputation: 78 2004-10-20 14:52:20 Score: 0 No inheritance code, but inherited the characteristics.
TOP
Reply to: J2NIX (August Qing Autumn) () Reputation: 100 2004-10-20 14:55:35 Score: 20 Example:
Door Door ---> Security door alert door (subclass), has anti-theft function (interface)
Door Door (parent) ---> Wood brain Wooden Door (subclass)
Anti-theft function is in addition to on the door, there may be on other things.
Therefore, the difference between abstract classes and interfaces is conceptually, which is conceptually focusing on things.
TOP
Reply to: zhaoqiubo (Little Bear Jeli Mei) () Reputation: 100 2004-10-20 15:00:47 Score: 0 Quote:
Door Door ---> Security door alert door (subclass), has anti-theft function (interface)
Door Door (parent) ---> Wood brain Wooden Door (subclass)
Anti-theft function is in addition to on the door, there may be on other things.
I managed to have anything else, anyway, there is no implementation process, and you don't have your own writing. What do I want to do this for this anti-theft function? ? ? In this way, I don't have to reduce the amount of code without an interface. .
Another: I didn't ask an abstract class. . .
TOP
Reply to: Haozhangpk () Reputation: 100 2004-10-20 15:01:27 Score: 10 In fact, the interface is a unified standard, the implementation of the specific class may be different, but the external name is the same. .
Let's take a look at the implementation of the COLLECTION and other descendants. I think you will understand.
There is still a little more, if it is used by the interface, it must be a tool class, which is the class called by the customer.
TOP
Reply to: zhaoqiubo (小 杰 米) () Reputation: 100 2004-10-20 15:01:40 Score: 0 Quote :: YZ790724 (Think)
There is no inheritance code, but inherits the characteristics.
You are afraid of making mistakes, and the inheritance of the interface is the main behavior.
TOP
Reply to: zhaoqiubo (小 杰 米) () Reputation: 100 2004-10-20 15:03:42 Score: 0 Interface is a standard
Ok, this is a bit. . However, this reason is not enough to let us use the interface. . Is there any more important reasons, let us use the interface to achieve multiple inheritance. . TOP
Reply to: zhaoqiubo (小 熊杰 米) () Reputation: 100 2004-10-20 15:07:31 Score: 0 to haozhangpk: Can you explain it in detail, this sentence?
There is still a little more, if it is used by the interface, it must be a tool class, which is the class called by the customer.
TOP
Reply to: caiyi0903 () Reputation: 100 2004-10-20 15:34:33 Score: 10 Interface is completely based on the design ideas.
Usually all generic methods, they need to implement them in specific classes.
Because some classes need to implement these common methods, it is necessary to inherit a rough class.
So the interface will be used to achieve multiple inheritance.
The example upstairs is listed very well :)
TOP
Reply to: zhaoqiubo (小 熊杰 米) () Reputation: 100 2004-10-20 15:42:42 Score: 0 to caiyi0903 (willpower) You may have not seen the question I asked, I didn't ask the interface. ? I didn't ask where to use it, I asked why it uses it, what is the benefits of using it, use it and does not use it?
TOP
Reply to: wwwer1 (Wuling Herger) () Reputation: 100 2004-10-20 16:50:30 Score: 0 No interface is difficult to achieve loose coupling.
I think this is the key to J2EE project.
There is not so many people who have no interface to drill Java.
Don't mention any example, look at the project you have done.
Take an ordinary small and medium-sized project, if the interface part is implemented in other ways, how much workload is required.
If you say that you don't need to modify or change, then you want your system to extend how much workload needs.
TOP
Reply to: Helldream2002 () Reputation: 100 2004-10-20 16:55:26 Score: 0 is of course easy to use
TOP
Reply to: iWenlong (Greedy Dragon) () Reputation: 100 2004-10-20 16:56:38 Score: 0 Recommend you to see Java design mode, implement those modes in Java, the interface is required, this is in a small program It is not easy to see, and the project is important.
TOP
Reply to: Tigeryu (Wu Yue Xiaohu) () Reputation: 97 2004-10-20 17:02:00 Score: 10 Interface As the name, mainly used to specify the external interface of the implementation. In this way, you have finished writing a program to find the interface to achieve the efficiency. You can replace it with another set, and what you do is to change a work class. Otherwise, you will go to the demise code. The most typical is JDBC. You have changed a database, then you call JDBC code is not changed, you want to change just a new driver class.
TOP
Reply to: YZ790724 (think about) () Reputation: 78 2004-10-20 17:02:06 Score: 0 Quote:
I managed to have anything else, anyway, there is no implementation process, and you don't have your own writing. What do I want to do this for this anti-theft function? ? ? In this way, I don't have to reduce the amount of code without an interface. .
============================================================================================================================================================================================================= ========== You are useless, but others are useful.
Have you used it to bind when running? Where is it, which anti-theft function of the interface is useful.
TOP
Reply to: zhaoqiubo (小 杰 米) () Reputation: 100 2004-10-20 17:13:18 Score: 0 I know it is useful, as far as light scalability, I can mention example, can also Say why. I asked what benefits using it to achieve multiple inherits, if you don't use anything? However, you are all clouds, and you can't say good truth. . Everyone discusses. . Useful to say useful, please take example.
Look at the attitude. . Look. . .啥 啥 "Useful". This is not strong. . Look at people to Tigeryu (Wu Yue Xiaohu) said how good.
TOP
Reply to: weimenren (quiet to the distance, indolence to express "() reputation: 152 2004-10-21 1:39:12 Score: 15 See the interface can not be static
1. Interface can be packaged in the underlying change
You know, Circle is Shape, and in this interface, you define all what you need, so you can do it, or Circle, you only need him to be Shape, you can operate
This encapsulates the change in the underlying implementation of the client.
2, the interface is conducive to the expansion of the system
It is also the example above, because the client, you open just Shape interface, when your Shape has increased or change time, you don't need to notify the client
An important one in OO is the programming of interface instead of implementation.
Because there is an interface to have a multi-state
With a polymorphic, there is a flexible dynamic binding.
In fact, you can say this: With an interface (abstraction), there is OO
TOP
Reply to: zhaoqiubo (Bear Jeli Mei) () Reputation: 100 2004-10-21 8:37:45 Score: 0 Something above, but I don't understand. I can also say these. Please see my problem carefully? I asked a multiple inheritance. .
TOP
Reply to: zhangjyr (tianium) () Reputation: 100 2004-10-21 9:15:24 Score: 10th floor owners in multiple inheritance actually do not understand Java's design ideas:
Java's design principle is not only familiar to programmers (so it uses C / C syntax) to avoid errors caused by language too complex (such as named memory leaks in C , for multiple inheritance, if There is the same method in both parent classes, which will cause compilation errors). Therefore, Java uses the concept of Interface to achieve multiple inheritance in another way. Introducing Interface in Java and said that it is based on the various advantages of the front of the big storytelling, but not, to avoid C multiple inheritance One of the problems used. (Don't misunderstand, this start should also have to make the system design in a logical more concise consideration), so I think the landlord understands that the original committee should be unnecessary to use multiple inheritance or interface.
TOP
Reply to: Flyforlove (for Element) () Reputation: 103 2004-10-21 9:23:56 Score: 20 This question is discussed before, in my opinion, the role of the interface is by no means for multiple inheritance,
The interface is a standard and is to reduce the coupling between the modules.
For a simple example, the collaboration between many people prepare different modules, if they are defined in advance,
As for who to implement the interface, it is not so important to use the interface, as long as the interface is defined,
Just do your own thing yourself.
TOP
Reply to: zhaoqiubo (Little Bear Jeli Mei) () Reputation: 100 2004-10-21 11:12:43 Score: 0 Multiple Inheritance, I can understand the extent to provide multiple standards, and implement object-oriented thought of. . As for another. . I can't understand it. . . Please make your guides.
TOP
Reply to: zzlove (annoying) () reputation: 100 2004-10-21 11:21:05 Score: 0 listening
TOP
Reply to: kaizaixian (kaizaixian) () Reputation: 98 2004-10-21 13:17:18 Score: 5 Interfaces can achieve multiple inheritance. Like the landlord, I don't understand how the interface is very good to achieve multiple inheritance? . But I think the interface is used to define some specification. To limit the objects to use the components. This is a bit similar to Flyforlove (for emotion). Perhaps the interface is not idealized for multiple inheritance, but this is just a feature that is not ideal for imagination of the shortcomings of C . (Personally
TOP
Reply to: joudor (flying bird) () reputation: 100 2004-10-21 13:31:07 Score: 5 huh, I give you a work instance:
It is necessary to make a tree to summarize the assembly, that is, the secondary developers can use this component to achieve data on the tree structure. But for me, I don't know what the tree structure is like, and I don't know how to summarize those data, and I don't know how to summarize.
If there is no interface, such a summary component can't do it, because you don't know what specific content now, but only know the structure.
TOP
Reply to: Joudor (Feixian Bird) () Reputation: 100 2004-10-21 13:42:46 Score: 10 Sorry, just missed the button, now there is full!
Oh, I will give you an instance of work:
It is necessary to make a tree to summarize the assembly, that is, the secondary developers can use this component to achieve data on the tree structure. But for me, I don't know what the tree structure is like, and I don't know how to summarize those data, and I don't know how to summarize. 1. If there is no interface, such a summary component can't do it, because you don't know what specific content now, and only know how to use the tree structure.
2. As is well known, the tree structure is divided into coding trees and primary key trees. If there is no multiple interface inherited, it has to be part of these two structures. And with multiple inheritance interfaces, this problem is good.
specific methods:
Define a basic summary interface as a parent class, and then define 2 interfaces inherit: A coded tree exchange interface, a primary key tree exchange interface. Then you think that the basic summary interface is the detail you want, there is a market structure, summarizing the data, type, content, summary method, etc. in the future, you can write a summary component.
When the secondary developer needs to call this component, as long as it is based on its own tree structure, it is a primary key tree, and then the aggregate component can be adjusted.
I have seen this example, you should understand it. Of course, this example is just the most basic multiple interface inheritance, the most is still in EJB, slowly understand, I wish you success soon!
TOP
Reply to: zhangjyr (tianium) () Reputation: 100 2004-10-21 13:45:10 Score: 10 Why do you have to put multiple inheritances and interfaces, they are just a function of language, what is important? Use these functions to architecture a system. We currently value the stripping of logical interfaces and achievements. If Java's Interface and C 's multiple inherits can also achieve the goal, but C does not completely, he still allows people to join the implementation in the interface, which makes the landlord doubt whether there is this benefit, in fact This problem is not a problem, any practices are good or bad (there are many very extreme Tricks when writing or c, which can improve efficiency, can think of these is destruction of programming specification, but from the perspective of the writer Look, this is the essence, the key is that the author wants to achieve the purpose, if it is to follow the principle of the interface and implementation, the language allows multiple inherited languages that allow multiple inheritances can be completely regardless of other features of multiple inheritance . If the landlord feels that multiple inheritance is very characterized from multiple types of inheritance, it does not have a complete feature in Java, but can still integrate multiple classes into a class in a design pattern such as Adapter. .
TOP
Reply to: Soyol () () Reputation: 100 2004-10-21 14:46:48 Score: 0 Example:
Door Door ---> Security door alert door (subclass), has anti-theft function (interface)
Door Door (parent) ---> Wood brain Wooden Door (subclass)
Anti-theft function is in addition to on the door, there may be on other things.
For interface, more pay more attention to achieving a behavior, with some processing capability
For inheritance, more focusing on the relationship between the things.
In implementations, an abstract class in multiple inheritance languages can also implement interface functions. But from understanding itself, the interface is more intended to describe behavioral characteristics.
example:
There are a variety of classes in the project.
However, some classes are required to be used. These classes implements a "verification authority" interface.
We can also define an abstract class such as "Version Class" and then inherit it, but don't turn. The interface is the most appropriate description.
Implement different classes of the same interface, they can be unrelated to each other
Inheriting different subclasses of the same father, they are brothers and sisters.
TOP
Reply to: Weimenren (quiet, indifferent, indolence) () reputation: 152 2004-10-21 18:58:49 Score: 20 Sorry, start an answer question without carefully see the description
I will first come to an example of multiple inheritance.
Interface Animal
Interface Landanimal Extends Animal {
Public void jump ();
}
Interface WateranImal Extends Animal {
Public void swim ();
}
Then how do you say Frog defined?
Frog can also swim, this time needs multiple inheritance this time.
Class Frog Implces Wateranimal, Landanimal {
}
But because of the time definition of the client, I use the Animal definition, I need to know what the object used by the client according to WATERANIMAL and LANDANIMAL.
Because this helps achieve polymorphism
Finally I need
(Wateranimal) .swim ();
(LANDANIMAL) .jump ();
Also in the last in the client I know that there is an animal that can jump or swim.
Regardless of how to inherit the interface or ask this abstraction of the interface, to facilitate the interface to program, reduce the coupling of the object
TOP
Reply to: lstkkk (虻 虻) () Reputation: 100 2004-10-22 6:22:16 Score: 0 listening
TOP
Reply to: Jiganghao (JH) () Reputation: 109 2004-10-22 7:13:48 Score: 0 Interface Can Be Used to Cope with The Problems of Multi Inheritance In C .
In C , you only have class supporting multi inheritance. The algorithm to decide which super class to inherit a method is complicated. You might need draw a hierarchy graph to find a class's nearest common super class. It is a pain when you traverse through tree Of super classes.
in Java, single inheritance enables one direction code reuse. it seems 'less' powerful. but coding is much simpler and easier to understand. In OO's view, inheritance is good for code reuse, but a big class hierarchy is very difficult to maintain and EXTEND.
An alternative of inheritance is composition. It is more flexible, and take much burden from inheritance. In OO, inheritance is a strict 'IS-A' relation. It must satisfy the substitution principle. Make your class hierarchy a pure 'IS-A 'Relation; Anything Else Should Uses Composition.top
Reply to: Jiganghao (JH) () Reputation: 109 2004-10-22 7:24:16 Score: 0 Interface Is A Typical Mechanism for Composition. In Java, Class Is Not The Preferrable Way To Define A Type, Interface IS.
when you have behavior from more than 1 classes to reuse, instead of multi inheritance in C , define 2 interfaces, each defines the set of behaviors you want. In Java, a class can implements more than 1 interface.
An examplar case: classAB wanna reuse behavior defined in classA and classB Define 2 interfaces:... InterfaceA implemented by ClassA, interfaceB implemented by classB Make classAB implements both interfaceA and interfaceB Make classAB containing 2 members: instanceA of type interfaceA, instanceB of interfaceB . For methods defined in Interfacea, Delegate The Method Calls to Instancea. Here We Replace Clas Inheritance With composition supported by interface.
DO ask me why comprs ptter tour inheritance. Ask your professor who teaches software engineering or ory.
TOP
Reply to: Jiganghao (JH) () Reputation: 109 2004-10-22 7:29:06 Score: 0 I Mean Do Not ASK ME Question About Inheritance and Composition.
TOP
Reply to: zhaoqiubo (Bear Jeli Mei) () Reputation: 100 2004-10-22 10:46:04 Score: 0 in Java, Class Is Not The Preferrable Way To Define A Type, Interface IS.
For methods defined in interfacea, delegate the method calls to instancea. Here We replace class inheritance with composition supported by interface. This is what it means. I don't understand, can you say? Examples I didn't understand ... Can you talk about it ..
TOP
Reply to: zhaoqiubo (Bear Jeli Mei) () Reputation: 100 2004-10-22 14:51:49 Score: 0 Who has time to help me translate translate?
TOP
Reply to: Weimenren (quiet, indolent, indifferent) () reputation: 152 2004-10-22 19:32:28 Score: 5 In Java, Interface is defined a type of preferred rather than Class
A method defines in Interface A, calling a method in instance A, where the combination of Interface replaces the inheritance of CLASS
TOP
Reply to: Jiganghao (JH) () Reputation: 109 2004-10-23 2:22:41 Score: 0 THX Weimenren.
TOP
Reply to: alal (~: cold ~: Winter: ~) () Reputation: 118 2004-10-23 11:09:34 Score: 0 to zhaoqiubo (Little Bear Jeli):
That is to say, the class is not suitable for defining a type, this work should be borne by the interface.
Why do I say this, I have this discussion in my post:
http://community.9cbs.net/expert/topic/3451/3451842.xml?temp=8.807009e-02
TOP
Reply to: zhaoqiubo (Bear Jeli Mei) () Reputation: 100 2004-10-25 9:19:45 Score: 0 This English guy, answering the interface and classes for a long time. . But I asked a multiple inheritance. . .
TOP
Reply to: Victorylj (Cherry) () Reputation: 100 2004-10-25 9:33:44 Score: 0 I also want to know, pay attention to ing
TOP
Reply to: BlackCourser (Be Stronger ...) () Reputation: 97 2004-10-25 16:22:04 Score: 0 Learning
TOP
Reply to: jeanven () () Reputation: 98 2004-10-25 19:53:42 Score: 0 Java itself does not support a class simultaneously inherited multiple classes, if you want to implement multiple classes, you can use the interface. achieve.
TOP
Reply to: xiaoyaozhifeng (遥 风) () Reputation: 100 2004-10-25 20:28:29 Score: 0 Efforts to learn, have seen more deep understanding of the interface, thank you
TOP
Reply to: cocolovevienna (exclusive) () Reputation: 94 2004-10-25 20:38:05 Score: 0 Weimenren (tranquility to stand away, indolence
The answer to this brother can answer multiple inheritance of human thinking logic.
TOP
Reply to: uglyugly (ugly ugly) () Reputation: 100 2004-10-26 9:33:41 Score: 0 "Design Mode" in the first chapter "To program the interface program", look at the design mode you will know Why is the interface.
I think the interface in "design mode" refers to a pure abstract class and interface class.
A little more inherited is that there will be a number of implementations in an interface, so there will be an interface.
TOP
Reply to: ZrHK (1 Clear Two White) () Reputation: 88 2004-10-26 10:16:40 Score: 0 There are many benefits: 1. In order to hide the subclasses, it plays the role of packaging.
2. Interface allows us to construct a non-hierarchical type frame
3. Can safely enhance a class of features: such as packaging classes
4. Java can achieve multiple inheritance through interfaces
and many more...
TOP
Reply to: zhaoqiubo (Bear Jeli Mei) () Reputation: 100 2004-10-27 13:18:00 Score: 0 batch. . No one in the end, no one can speak multiple inheritance benefits. I estimate that it is defined a number of criteria and matching ideas. . .