90 likes | 263 Views
Thinking in Objects. Chapter 10. Immutable Objects and Classes. The String class is immutable. How to make our Circle class immutable? delete the set method! radius is private, cannot be changed without a set method. Scope of Variables. class variable vs . local variable
E N D
Thinking in Objects Chapter 10
Immutable Objects and Classes • The String class is immutable. • How to make our Circle class immutable? • delete the set method! • radius is private, cannot be changed without a set method
Scope of Variables • class variable vs. local variable • publicclass Foo { • privateintx = 0; //Class variable • privateinty = 0; • publicvoid p(){ • intx = 1; //Local variable • System.out.println("x = " + x); • System.out.println("y = " + y); • } • }
The this Keyword • The this keyword is the name of a reference that refers to an object itself. • reference a class’s hidden data fields
The this Keyword • enable a constructor to invoke another constructor of the same class
Class Abstraction and Encapsulation • Class abstraction means to separate class implementation from the use of the class. • The detail of implementation is encapsulated and hidden from the user. • developer vs. user
The Loan Class • UML (Unified Modeling Language) diagram
The BMI Class • BMI = weight (in kilograms) / height2 (in meters) • BMI Interpretation • below 16 seriously underweight • 16-18 underweight • 18-24 normal weight • 24-29 overweight • over 29 seriously overweight