Chapter 10 Thinking in Objects 1 Immutable Objects
Chapter 10 Thinking in Objects 1
Immutable Objects and Classes If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method. Immutable means unchangeable. In Java, when an object is defined as being immutable it means that once it has been initialized its state cannot be changed. Primitive data types (i. e. , int, short, long, byte, char, float, double, boolean) can be made immutable by using the "final" keyword. Once they have been assigned a value it cannot be changed. 2
Example 3
The this Keyword F The this keyword is the name of a reference that refers to an object itself. F One common use of the this keyword is reference a class’s data fields. F Another common use of the this keyword to enable a constructor to invoke another constructor of the same class. 4
this keyword Sometimes a method will need to refer to the object that is calling object itself. F this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. F To better understand what this refers to, consider the following version of Box( ): F // An example use of this. Box(double w, double h, double d) { this. w = w; this. h = h; this. d= d; } 5
Reference the Hidden Data Fields The this keyword provides a way to refer to the objhect that called an instance method within the code of method. • The line this. i=i means assign the value that calls the instances method set. I. • The Foo. k=k means that the value in parameter k is assigned to the static data field k of the class. 6
Calling Overloaded Constructor 7
Class Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. 8
Designing the Loan Class Test. Loan. Class Loan 9
Object-Oriented Thinking This section improves the solution for a problem introduced in Chapter 3 using the object-oriented approach. Problem: Computing Body Mass Index (BMI) – Listing 3. 5 From the improvements, you will gain the insight on the differences between the procedural programming and object-oriented programming and see the benefits of developing reusable code using objects and classes. 10
The BMI Class BMI Use. BMIClass 11
Example: The Course Class Course Test. Cource 12
Case Study: Designing the Course Class Test. Course & Course public class Test. Course { public static void main(String[] args) { Course course 1 = new Course("Data Structures"); Course course 2 = new Course("Database Systems"); public class Course { private String course. Name; private String[] students = new String[100]; private int number. Of. Students; /*A Course object can be created using the constructor Course (String public Course(String course. Name) { name) by passing a course name. */ this. course. Name = course. Name; course 1. add. Student("Peter Jones"); } course 1. add. Student("Brian Smith"); public void add. Student(String student) { course 1. add. Student("Anne Kennedy"); students[number. Of. Students] = student; number. Of. Students++; course 2. add. Student("Peter Jones"); } course 2. add. Student("Steve Smith"); public String[] get. Students() { return students; System. out. println("Number of students in course 1: " } + course 1. get. Number. Of. Students()); public int get. Number. Of. Students() { String[] students = course 1. get. Students(); return number. Of. Students; for (int i = 0; i < course 1. get. Number. Of. Students(); i++) } System. out. print(students[i] + ", "); public String get. Course. Name() { return course. Name; System. out. println(); } System. out. print("Number of students in course 2: " public void drop. Student(String student) { + course 2. get. Number. Of. Students()); // Left as an exercise in Exercise 9. 9 } } 13
Example: The Stack. Of. Integers Class Test. Stack. Of. Integers 14
Designing the Stack. Of. Integers Class 15
Implementing Stack. Of. Integers Class Stack. Of. Integers 16
- Slides: 16