How to identify classes?
- Look for nouns in the problem analysis . This could be good candidates as class
- Look for verbs in the problem analysis, this could be good candidates as method
Relationships between classes:
- Dependence => "uses a"
- Aggregation => "has a"
- Inheritance => "is a"
Class Design Hints:
1. Always keep data private
2. Always initialize data
3. Don't use too many basic types in a class
- Replace multiple related uses of basic types with other classes. This keeps your classes easier to understand and to change.
4. Not all fields need individual field accessors and mutators.
5. Break up classes that have too many responsibilities
6. Make the names of your classes and methods reflect their responsibilities.
Inheritance Hints:
1. Apply "is-a"
2. It is best to minimize the use of casting and the instanceof operator.
3. In practice use protected fields with caution and try to avoid it because it is against the spirit of OOP, which encourages data encapsulation.
3. Protected method makes more sense. A class may declare a method as protected if it is tricky to use. This indicates that the subclass can be trusted to use this method correctly, but other classes are not trusted to use this method.
Summary of JAVA access modifiers that control visibility:
1. visible to the class only => private
2. visible to the world => public
3. visible to the package and all sub classes => protected
4. visible to the package => default, No modifiers are required.
Design hints for OO inheritance
1. Place common operations and fields in the super class
2. Don't use protected fields => This can break OO encapsulation. a. any subclass can access/modify such fields; b. Any class in the same package can access/modify such fields
3. Use inheritance to model "is-a" relationship.
4. Don't use inheritance unless all inherited methods make sense.
5. Don't change the expected behavior when you override a method. => this violates the substitution principle.
6. Use polymorphism, not type information => avoid instanceof as much as possible
7. Don't overuse reflection => Extremely useful for system programming , but it is not appropriate for applications. Reflection is fragile.