Thinking in Java (Chapter 9)
===
###### tags: `Java` `OOP`
1. A class containing abstract methods is called an abstract class
1. For C++ programmers, this ist he analogue of C++’s pure virtual function
1. 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.
1. It’s possible to make a class abstract without including any abstract methods
1. It’s helpful to create abstract classes and methods because they make the abstractness of a class explicit
1. 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
1. The interface is used to establish a "protocol" between classes
1. The interface allows you to perform a variation of "multiple inheritance" by creating a class that can be upcast to more than one base type.
1. An interface can also contain fields, but these are implicitly static and final
1. To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword
1. You can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name). If you leave off the public keyword, you get package access
1. Creating a method that behaves differently depending on the argument object that you pass it is called the Strategy design pattern
2. the core reasons for interfaces
1. To prevent the client programmer from making an object of this class and to establish that it is only an interface.
1. If it’s possible to create your base class without any method definitions or member variables, you should always prefer interfaces to abstract classes
1. ‘virtual’ keyword is the solution of diamond problem in C++ to prevent ambiguities
1. extends can refer to multiple base interfaces when building a new interface.
1. a common use for interfaces is the aforementioned Strategy design pattern
1. 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. The fields in an interface are automatically public, so that is not explicitly specified
1. Fields defined in interfaces cannot be "blank finals," but they can be initialized with non constant
1. implementing a private interface is a way to force the definition of the methods in that interface without adding any type information(without allowing any upcasting)
2. when you implement an interface, you are not required to implement any interfaces nested within.
1. Also, private interfaces cannot be implemented outside of their defining classes.
1. A a = new A(); // Can’t access A.D: //! A.D ad = a.getD(); // Doesn’t return anything but A.D: //! A.DImp2 di2 = a.getD(); // Cannot access a member of the interface: //! a.getD().f(); // Only another A can
1. Factory Method design pattern
1. prefer classes to interfaces. Start with classes, and if it becomes clear that interfaces are necessary, then refactor
2.