Issues relating to interfaces and abstract classes

1. If a sub-class implements an interface, on all methods (whether or not required) interface must be implemented; if it is inheriting an abstract class need only implement the required method can be, which is an advantage abstract class

2. If a method name defined in the interface has changed, then all of a subclass implements this interface will obviously be unable to compile because they implemented method name does not exist, which is a disadvantage interface; and an abstract class that does not exist problem, just add a new method (the old method Interface) is a subclass of

3. see the previous two, it seems more abstract than the interface has advantages, but it has a irreparable disadvantage: that is a subclass can have only one parent. A B the extends. Thus, it has all methods A and B of the functions, but would like the time A have the function C. It can not be achieved by A extends C, and need to take some detours. At present the system architecture is a trend for abstract (excuse abstract class) rather than a specific program, and will function as a subdivision. This needs to be achieved by means of implementing multiple interfaces, obviously, the abstract class can not provide such functionality. Reconstructed from a system perspective, an abstract interface to a particular class is very convenient. Write only one interface, all the methods which defines concrete classes, then in this particular class is the interface to implement it. The abstract class complex will more than, say, B extends A, C extends B c if you want to abstract an abstract class D, then it needs to find its top-level A to start from scratch, because they can not do C extends D
-------------------------------
inherit the abstract class when not required to implement all of the methods, but this is a subclass of abstract , can not be instantiated
-------------------------------
I mean also make use interface, but an abstract class is not to say useless, should each have their own advantages. The best way is to write an interface, and then do a default this abstract class that implements the interface, or inherit an abstract class that implements the interface be selected as desired, but the disadvantage is to write too much class
---------- ---------------------
advantage abstract class: B inherits a, C inherited a, assumed class B, C fun1 inherited implementation code is the same as the method of , the code can be written in the a, a BC simply call the parent class method can, without re-writing code; BC and other methods were the same, the specific implementation is not the same code, then written a method abstract.
-------------------------------
sentence good words, and so on, constantly extends new abstract class. Our new method, for different classes to inherit achieve improved reusability of code, but the drawback is too rigid, targeted some
--------------- ----------------
to implement the interface isolation can only be achieved through the interface, which is the biggest flaw of abstract
---------------- ---------------
interface multiple inheritance, the Earth people know that stuff is in the interface of the public.
-------------------------------
tidy landlord posted:

1. a sub-class implements an interface, if the subclass a non-abstract class for all methods (whether it is needed) must implement the interface; if the sub-class is an abstract class

, the interface may be implemented in the method may not be achieved.
   A Subclasses inherit an abstract class, if the sub-class of non-abstract class, must all abstract base class method implementation; and if the sub-class is an abstract class, it can be realized

in abstract method now base class may be not achieve.
   // both cases look the same, but there may be non-abstract method abstract classes, and interfaces must be all abstract methods.

2. If the name of a method defined in the interface has changed, then all subclasses implement this interface will obviously be unable to compile because that method is not real renamed in non-abstract subclass interface

now.
   If you modify an abstract class abstract method name, a subclass of the same compiler, however, but if non-abstract method to modify, compile without problems.
   // Both seem to see is about the same, but the method to modify the abstract and non-abstract of difference.

3. With regard to
   the name of an abstract class if you modify an abstract method, the same subclass compiled However, if non-abstract method to modify, compile without problems, this uncertainty is precisely the abstract class of fatal

weakness.

--------------------------------

   how we could not pass it? Initial named aaa, a subclass inherits a method of aaa, when the abstract class instead of aaa bbb, aaa subclass would have, and two methods bbb

   // initially named aaa (name should be abstract.), subclass inherits (and implement) the method of aaa, when the abstract class instead of aaa bbb, aaa subclass would have, and two methods bbb (sub-

class then just have a new method for aaa, and abstract classes of abstract methods bbb not implemented in a subclass, of course, can not compile)
--------------------------------- -------
I found that we too one-sided understanding of the interface, the interface can be understood: it is not just a class, it is a norm ~ ~ ~

like a system, there have been older members are replaced by new members, but the system can still work well, and why? It is because the members have to comply with a standardized system so that the normal operation of the software, the interface would play such a role, it does not replace the members do specific work (coding) but to tell members what to do (interfaces method)

and then some of the details, that is to interface methods defined only parameter types, return type, method name, is to tell members what kind of work done with what kind of tools (method name) what resources (parameter type) (return value type)

based on the starting point, a few or even unrelated public abstract class interface, the abstract class interface specification that is

that the most important role is the interface, a better interface is coupled through the system integrity may be maintained and sex, scalability
-----------------------------------------
but the interface will potentially increasing at least one class, if coupled with an abstract isolation, then it is more than two out. If other people also understand this model okay if other people do not understand it? It greatly increases the complexity of the structure of FIG class (general class number of initial system originally few).
----------------------------------------
that use interfaces and abstract classes will reduce the amount of code Another misunderstanding is

a dependent abstraction layer system in the development process, will inevitably change to change to the abstraction layer and implementation layer, the workload will even increase exponentially
The benefits of such a system in the latter part of the maintenance and development again is obvious, do change a lot less will be easier, a lot of people have this experience: When adding a function of time for a program suddenly found some other function so bad, and if a layer of abstraction well-designed system will not be the case, but this will depend on the previous design abstraction layer prediction of possible situations is comprehensive, otherwise, both the interface and change the code change is undoubtedly a nightmare, it is a question of the amount of experience designer.

These are my current understanding of the interface, let me give some specific examples I can give out, only you can not write, you can remember what I said about the experience in doing these projects in the future course of
---- -----------------------------
in fact, I personally believe that we are carrying OO design, we analyze the more detailed the more increase the precision of the code we use. At this time, we must have a detailed understanding of all of the framework to begin. This is a very good user experience.
--------------------------------
most difficult to achieve new theory to guide practice, so for newcomers, What you want to use what good. . . Ensure the program can turn on the line. Wait until practice more, so feel uncomfortable writing program begins, it will naturally uncomfortable to think about why. . If you have not felt uncomfortable, so it was. .

Reproduced in: https: //www.cnblogs.com/200831856/archive/2009/11/25/interface_abstract.html

Guess you like

Origin blog.csdn.net/weixin_34090562/article/details/93711383