130 likes | 270 Views
Chapter 10. OOPS!. Immutable Objects. All data fields are private No mutator methods No accessor method that returns a reference to a mutable data field. Scope. Instance and static variables are visible everywhere in the class
E N D
Chapter 10 OOPS!
Immutable Objects • All data fields are private • No mutator methods • No accessor method that returns a reference to a mutable data field
Scope • Instance and static variables are visible everywhere in the class • Local variables declared inside methods are visible within the method (or block of code if declared inside braces) • A local variable with the same name as a class instance or static variable will mask the class variable while the local variable is in scope!
this • The this reference is used to access the object that is calling the method • The this reference can be used to access hidden variables • The this reference can be used to call constructors
Principles of OO design • Encapsulation means keeping the implementation private from the outside world • Abstraction means separating the implementation of the class from the use of the class • Both are enforced by using an interface contract through the class methods
Is-a, Has-a, and extends • Has-a is an ownership relationship between two classes where one class is part of, or subordinate to, another class • Is-a is an extension or refinement relationship between two classes where one class is a type or subset of another class • The extends keyword defines the Is-a relationship in terms of inheritance
Examples • Wave hands and draw on white board here
Class Design Principles • Cohesion means designing a class to represent one entity “well” with methods and data the logically and coherently support its purpose • Consistency means choosing informative names and logical layout of classes (e.g. data declarations, then constructors, then methods in class definition).
Ad nauseaum • Encapsulation means keeping the implementation private and accessible through appropriate methods • Clarity means creating a clear and concise user interface “contract” for using the class • Completeness means providing all the necessary and appropriate methods for using the class and nothing more (or less)
Static variables • Static variables should only be declared for values that need to be shared among all instances of a class. • Typically instance counter or a global resource flag • Accessed using the class_name.static_value