1 / 13

Chapter 10

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

justus
Download Presentation

Chapter 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 OOPS!

  2. Immutable Objects • All data fields are private • No mutator methods • No accessor method that returns a reference to a mutable data field

  3. 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!

  4. 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

  5. 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

  6. 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

  7. Examples • Wave hands and draw on white board here

  8. 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).

  9. 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)

  10. 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

More Related