Java Knowledge series 5

Interface from user, not from implementor.(DIP)

Interface-Oriented Programming.

Interface or Abstract function or Abstract class

  1. Java专门提供了一种机制,名为“抽象方法”(same as abstraction)。它属于一种不完整的方法,只含有一个声明declaration,没有方法主体 body of definition。下面是抽象方法声明时采用的语法:abstract void X();包含了抽象方法的一个类叫作“抽象类”abstract class with one or more abstract functions。
  2. 如果一个类里包含了一个或多个抽象方法,类就必须指定成abstract(抽象)。
  3. 如果从一个抽象类继承,而且想生成新类型的一个对象,就必须为基础类中的所有抽象方法提供方法定义。如果不这样做(完全可以选择不做),则衍生类derived也会是抽象的,而且编译器会强迫我们用abstract关键字标志那个类的“抽象”本质。
  4. 即使不包括任何abstract方法,亦可将一个类声明成“抽象类”。如果一个类没必要拥有任何抽象方法,而且我们想禁止那个类的所有实例,这种能力就会显得非常有用。User can’t create a instance of abstract class.
  5. interface”(接口pure abstract class)关键字使抽象的概念更深入了一层。我们可将其想象为一个“纯”抽象类。它允许创建者规定一个类的基本形式:方法名、自变量列表以及返回类型,但不规定方法主体。接口也包含了基本数据类型的数据成员,但它们都默认为static和final。
  6. 接口只提供一种形式,并不提供实施的细节。接口这样描述自己:“对于实现我的所有类,看起来都应该象我现在这个样子”。
  7. It is impossible to create a instance of abstract class or interface.
  8. 这便是接口的全部含义。所以我们常把接口用于建立类和类之间的一个“协议”。有些面向对象的程序设计语言采用了一个名为“protocol”(协议)的关键字,它做的便是与接口相同的事情。
  9. 为创建一个接口,请使用interface关键字,而不要用class。与类相似,我们可在interface关键字的前面增加一个public关键字(但只有接口定义于同名的一个文件内);或者将其省略,营造一种“友好的”状态。为了生成与一个特定的接口(或一组接口)相符的类,要使用implements(实现)关键字。我们要表达的意思是“接口看起来就象那个样子,这儿是它具体的工作细节”。
  10. Public interface TestListener{}
  11. Public interface Receiver{}
  12. public abstract class BaseTest implements TestListener,Receiver{}
  13. public class DerivedTest extends BaseTest implements Clonable, Runnable, Comparable{}
  14. 由于接口根本没有具体的实施细节——也就是说,没有与存储空间与“接口interface”关联在一起——所以没有任何办法可以防止多个接口合并到一起。这一点是至关重要的,因为我们经常都需要表达这样一个意思:“x从属于a,也从属于b,也从属于c”。在C++中,将多个类合并到一起的行动称作“多重继承”multiple inheritance,而且操作较为不便,因为每个类都可能有一套自己的实施细节。在Java中,我们可采取同样的行动,但只有其中一个类拥有具体的实施细节。
  15. 如果确实想从一个非接口继承,那么只能从一个继承。剩余的所有基本元素都必须是“接口”。我们将所有接口名置于implements关键字的后面,并用逗号分隔它们。
  16. 利用继承技术,可方便地为一个接口添加新的方法声明,也可以将几个接口合并成一个新接口。
  17. 由于置入一个接口的所有字段都自动具有static和final属性 in interface,所以接口是对常数值进行分组的一个好工具,它具有与C或C++的enum非常相似的效果。
  18. 接口中定义的字段会自动具有static和final属性。它们不能是“空白final”,但可初始化成非常数表达式。由于字段是static的,所以它们会在首次装载类之后、以及首次访问任何字段之前获得初始化。
  19. The interface, remember, automatically makes all of its members public.
  20. Interface and inner classes provide more sophisticated ways to organize and control the objects in your systems.
  21. The abstract keyword, allows you to create one or more methods in a class that have no definitions- you provide part of the interface without providing a corresponding implementation, which is created by inheritors.
  22. The interface keyword produces a completely abstract class, one that provides no implementation at all. You will learn that the interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++’s “multiple inheritance” by creating a class that can be upcast to more than one base type.
  23. interface in COM is defined as struct to keep a lot of ‘public’ lines out of the interfaces.
  24. The interface in Java takes the abstract concept one step further. You could think of it as a “pure” abstract class (like depicted in C++).
  25. Interface allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies (which shall be given in concreate class that implements this interface).
  26. An interface can also contain fields, but these are implicitly static and final (pertain to all object of the class and never be changed).
  27. An interface provides only a form, but no implementation. (What placement means).
  28. An interface says, “This is what all classes that implement this particular interface will look like.”
  29. The interface is used to establish a “protocol” between classes. (some object-oriented programming languages have a keyword called protocol to do the same thing.)
  30. OOP  MDD ( Model-Driven Design)  IOD ( Inteface-Oriented Design)  DDD (Domain-Driven Design) PDD ( Pattern-Oriented Design)
  31. SOA:Service-Oriented Architecture
  32. To create an interface, use the interface keyword instead of the class keyword. Like a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name) or leave it off to give package access, so that it is only usable within the same package.
  33. To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword, which says, “The interface is what it looks like, but now I am going to say how it works.”
  34. You can choose to explicitly declare the method as public in an interface, but they are public even if you don’t do that. The methods in interface have a defacult access control “public”.
  35. When you implement an interface, the methods from the interface must be defined as public. Otherwise, they would defacult to package access (friendly), and you had be reducing the accessibility of a method during inheritance, which is not allowed by the Java compiler.
  36. The variants in interface are static and final. Which means they are compile-time constant.
  37. This is the intent: Each approach gives the programmer different control over the way objects are created and used.
  38. “Multiple inheritance” in Java: The interface is not simply a “more pure” form of abstract class. It has a higher purpose than that. (So same as interface in COM). Because an interface has no implementation at all, that is, there is no storage associated with an interface-there is nothing to prevent many interfaces from being combined.
  39. Interface just look like a placement for the concreate class in order to make JVM know what methods the class must and have to have.
  40. In a derived class, you are not forced to have a base class that is either an abstract or ‘concrete’ ( one with no abstract methods). If you do inherit from a non-inferface, you can inherit from only one. All the rest of the base elements must be interfaces. You place all the interface names after the implements keyword and separate them with commas.
  41. You can have as many interfaces as you want; each one becomes an independent type that you can upcast to.
  42. In concrete class with all definitions of methods of interface provided, all methods which are in implemented interfaces shall be provided definition (body), no matter the definition of method is gaven in this class or its base class. Just having one definition is OK.
  43. A reason for using interfaces is the same as using an abstract base class: to prevent the client programmer from making an object of this class and to establish that it is only an interface.
  44. An interface gives you the benefits of an abstract class and the benefits of an interface, so if it’s possible to create your base class without any method definitions or member variables, you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, your first choice should be to make it an interface, and only if you’re forced to have method definitions or member variables should you change to an abstract class, or if necessary a concrete class.
  45. Name collisions when combining interfaces: if a few of interface has the method with same name, but different argument type or different return type, what will happen?
  46. The difficulty occurs because overriding, implementation, and overloading get unpleasantly mixed together, and overloaded methods can not differ only by return type. When the last two lines are uncommented, the error messages it all:
  47. Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well. Strive to avoid it.
  48. Extending an interface with inheritance: You can easily add new method declarations to an interface by using inheritance, and you can also combine several interfaces into a new interface with inheritance. In both cases you get a new interface.
  49. Grouping constants: because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C++.
  50. Notice the Java style of using all uppercase letters ( with underscores to separate multiple words in a single identifier) for static finals that have constant initializers.
  51. The fields in an interface are automatically public, so it is unnecessary to specify that.
  52. You can use the constants from outside the package by importing c08.* or c08.Months just as you would with any other package, and referencing the values with expressions like Months.JANUARY. Of course, what you get is just an int, so there isn’t the extra type safety that C++’s enum has, but this (commonly used) technique is certainly an improvement over hard coding numbers into your programs. (That approach is often referred to as using “magic numbers,” and it produces very difficult-to-maintain code.)
  53. Initializing fields in interfaces: Fields defined in interfaces are automatically static and final. These can not be “blank finals”, but they can be initialized with non-constant expressions. Since the fields are static, they are initialized when the class is first loaded, which happed when any of the fields are accessed for the first time.
  54. The fields, of course, are not part of the interface but instead are stored in the static storage area for that interface.
  55. Nesting interfaces: interfaces may be nested within classes and within other interfaces.
原文地址:https://www.cnblogs.com/iiiDragon/p/3241774.html