1. The class must document precisely the effects of overriding any method. In other words, the class must document its self-use of overridable methods.
2. By convention, a method that invokes overridable methods contains a description of these invocations at the end of its documentation comment. The description begins with the phrase “This implementation”. This phrase should not be taken to indicate that the behavior may change from release to release.
3. To allow programmers to write efficient subclasses without undue pain, a class may have to provide hooks into its internal workings in the form of judiciously chosen protected methods or, in rare instances, protected fields. You should expose as few protected members as possible, because each one represents a commitment to an implementation detail.
4. The only way to test a class designed for inheritance is to write subclasses. Experience shows that three subclasses are usually sufficient to test an extendable class. One or more of these subclasses should be written by someone other than the superclass author.
5. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will get invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.
6. If you decide to implement Cloneable or Serializable in a class designed for inheritance, you should be aware that because the clone and readObject methods behave a lot like constructors, a similar restriction applies: neither clone nor readObject may invoke an overridable method, directly or indirectly. In the case of the readObject method, the overriding method will run before the subclass’s state has been deserialized. In the case of the clone method, the overriding method will run before the subclass’s clone method has a chance to fix the clone’s state. In either case, a program failure is likely to follow. In the case of clone, the failure can damage the original object as well as the clone. This can happen, for example, if the overriding method assumes it is modifying the clone’s copy of the object’s deep structure, but the copy hasn’t been made yet.
7. If you decide to implement Serializable in a class designed for inheritance and the class has a readResolve or writeReplace method, you must make the readResolve or writeReplace method protected rather than private. If these methods are private, they will be silently ignored by subclasses.
8. Prohibit subclassing in classes that are not designed and documented to be safely subclassed. There are two ways to prohibit subclassing. The easier of the two is to declare the class final. The alternative is to make all the constructors private or package-private and to add public static factories in place of the constructors.
9. You can eliminate a class’s self-use of overridable methods mechanically, without changing its behavior. Move the body of each overridable method to a private “helper method” and have each overridable method invoke its private helper method. Then replace each self-use of an overridable method with a direct invocation of the overridable method’s private helper method.
相关推荐
Item 19: Design and document for inheritance or else prohibit it Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity Item 22: Use interfaces only to define types ...
> Design and document for inheritance or else prohibit it > > – Item 19, Effective Java, Third Edition Usage Java creates new types as open by default which can be dangerous. This checker ensures ...
Object Pascal Handbook This is the book summary Table of Contents: Part I Chapter 1: Coding in Pascal Chapter 2: Variables and Data Types ...Chapter 17: The TObject Class Chapter 18: RunTime Library
- **Inheritance and Polymorphism:** These are advanced OOP concepts that allow for code reuse and flexibility. Inheritance enables one class to inherit properties and behaviors from another. ...
**Item 17: Implement the Standard Dispose Pattern** - **Pattern:** Ensures proper disposal of unmanaged resources. - **Implementation:** ```csharp public class MyDisposable : IDisposable { ...
Chapter 17: Processes, AppDomains, and Object Contexts Chapter 18: Understanding CIL and the Role of Dynamic Assemblies Part VI: Introducing the .NET Base Class Libraries Chapter 19: Multithreaded, ...
Most important, basing inheritance on JavaScript’s prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance. What You’ll Learn What are objects, ...
Each chapter that follows describes one of twenty-three design patterns, recommends when to use it, and explains the impact that it will have on the larger design. The use of every pattern is ...
Chapter 17: Working with Dialogs and Controls Chapter 18: Storing and Printing Documents Chapter 19: Writing Your Own DLLs Chapter 20: Connecting to Data Sources Chapter 21: Updating Data Sources ...
"Common Sense C: Advice and Warnings for C and C++ Programmers" is a valuable resource for anyone involved in C or C++ development. From identifying common mistakes to offering practical solutions and...
It is completely platform independent in that tests built for one browser or mobile device, will also work on all other browsers and mobile devices. Selenium supports all major development languages ...
- **Logical Operators**: AND, OR, and NOT operators for boolean expressions. - **Bitwise Operators**: Performing bitwise operations on binary data. - **Ternary Operator**: A shorthand for if-else ...
Chapter 17: Threading Chapter 18: Graphical User Interface) Chapter 19: Graphical User Interface) Chapter 18: Event Handling Chapter 19: JDBC Programming Chapter 20: Exception Handling Chapter 21: ...
2.12.1. and, or, and not 2.13. while Loops and Recursion 2.13.1. while 2.13.2. car, cdr, cons: Fundamental Functions 2.13.3. while, Continued 2.13.4. Recursion 2.14. Macros 2.14.1. The list ...
- **Algorithm Design**: Discussion of algorithm design principles and techniques for analyzing the efficiency of algorithms. **Programming Project 9.6**: This project likely involves implementing ...