`
leonzhx
  • 浏览: 768192 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1) An abstract method is a method that is incomplete. It has only a declaration and no method body. Here is the syntax for an abstract method declaration: abstract void f();

 

2) A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class itself must be qualified as abstract.

 

3) If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword.

 

4) It’s possible to make a class abstract without including any abstract methods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstract methods, and yet you want to prevent any instances of that class.

 

5) The interface keyword produces a completely abstract class, one that provides no implementation at all. It allows the creator to determine method names, argument lists, and return types, but no method bodies. An interface provides only a form, but no implementation.

 

6) An interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation of "multiple inheritance" by creating a class that can be upcast to more than one base type.

 

7) To create an interface, use the interface keyword instead of the class keyword. An interface can also contain fields, but these are implicitly static and final.

 

8) 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’m going to say how it works." Other than that, it looks like inheritance.

 

9) You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it. So when you implement an interface, the methods from the interface must be defined as public.

 

10)  Java compiler dosen't allow you to reduce the accessibility of a method during inheritance.

 

11) The split( ) method is part of the String class. It takes the String object and splits it using the argument as a boundary, and returns a String[ ].

 

12) Creating a method that behaves differently depending on the argument object that you pass it is called the Strategy design pattern.

 

13) You are often in the situation of not being able to modify the classes that you want to use. For example, the library was discovered rather than created. In these cases, you can use the Adapter design pattern. In Adapter, you write code to take the interface that you have and produce the interface that you need. ( usually using delegation )

 

14) If you do inherit from a non-interface, 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. When you combine a concrete class with interfaces this way, the concrete class must come first, then the interfaces.

 

15) One of the core reasons for interfaces is: upcast to more than one base type (and the flexibility that this provides). However, a second 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.

 

16) extends can refer to multiple base interfaces when building a new interface and the base interface names are simply separated with commas.

 

17) You cannot inherit from one base class and some interfaces that have the same funciton signature but with imcompatible return type.

 

18) A common use for interfaces is the Strategy design pattern. You write a method that performs certain operations, and that method takes an interface that you also specify. You’re basically saying, "You can use my method with any object you like, as long as your object conforms to my interface." This makes your method more flexible, general and reusable. A method that takes an interface provides a way for any class to be adapted to work with that method. This is the power of using interfaces instead of classes.

 

19) Because any fields you put into an interface are automatically public, static and final, the interface is a convenient tool for creating groups of constant values.

 

20) Fields defined in interfaces cannot be "blank finals", it even can't have any initializers (static or non-static ).

 

21) Interfaces may be nested within classes and within other interfaces. interface itself is implicitly static, so it cannot be defined in non-static inner class.

 

22) Just like non-nested interfaces, a nested interface in a class can have public or package-access visibility. And it can also be private, but this interface can only be accessed within its defining class.

 

23) Interfaces can be nested within each other. However, the rules about interfaces—in particular, that all interface elements must be public—are strictly enforced here, so an interface nested within another interface is automatically public and cannot be made private.

 

24) Factory Method design pattern: instead of calling a constructor directly, you call a creation method on a factory object which produces an implementation of the interface—this way, in theory, your code is completely isolated from the implementation of the interface, thus making it possible to transparently swap one implementation for another.

 

25) An appropriate guideline is to prefer classes to interfaces. Start with classes, and if it becomes clear that interfaces are necessary, then refactor.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics