Download presentation
Presentation is loading. Please wait.
Published byBaldwin Wiggins Modified over 9 years ago
1
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
2
Exam Next Monday, March 26 th Same time (11:45 – 12:35), same location Chapters 7, 8, 10 No HW, No PA this week Announcements
3
Chapter 10 Immutable objects this keyword Composition Differences between procedural programming & OOP Guidelines for OOP Objectives
4
More on Objects
5
Immutable object: If the contents of an object cannot be changed once the object is created Its class is called an immutable class. Immutable Circle3
6
A class with all private data fields and without mutators is not necessarily immutable. Example: next slide (Student class) Immutable
7
Example.. public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear,int newMonth,int newDay) { year = newYear; month = newMonth; day = newDay; } public void setYear(int newYear) { year = newYear; } }
8
Example public class Student { private int id; private BirthDate birthDate; public Student(int ssn,int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); } public int getId() { return id; } public BirthDate getBirthDate() { return birthDate; } }
9
Example … 9 public class Test { public static void main(String[] args) { Student student = new Student(111223333,1970, 5, 3); BirthDate date = student.getBirthDate(); date.setYear(2010); // Now the student birth year is // changed! } }
10
For a class to be immutable, it must 1. Mark all data fields private 2. Provide no mutator methods 3. Provide no accessor methods that would return a reference to a mutable data field object. Immutable
11
Scope
12
The scope of instance and static data fields is the entire class. They can be declared anywhere inside a class. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used. Variable Scope
13
Example Variable Scope
14
If a local variable has the same name as a class’s variable, the local variable takes precedence and the class’s variable with the same name is hidden. Variable Scope
15
Example Variable Scope
16
this Keyword
17
The this keyword is the name of a reference that refers to an object itself. One common use of the this keyword is reference a class’s hidden data fields. Another common use of the this keyword to enable a constructor to invoke another constructor of the same class. this
18
Using this to reference hidden fields this
19
Use this to call overloaded constructor this
20
Composition & Aggregation
21
An object can contain another object. The relationship between the two is called composition. Composition
22
Composition is a special case of the “aggregation” relationship. Aggregation models “has-a” relationships. The owner object is called an aggregating object. The subject object is called an aggregated object. Aggregation
23
An object may be owned by several other aggregating objects. If an object is exclusively owned by an aggregating object, the relationship between them is referred to as “composition”. Composition
24
“a student has a name” A composition relationship “a student has an address” An aggregation relationship An address may be shared by several students. Example
25
UML composition & aggregation notation UML
26
Each class involved in a relationship may specify a multiplicity. A multiplicity could be a number or an interval that specifies how many objects of the class are involved in the relationship. The character * means an unlimited number of objects The interval m..n means that the number of objects should be between m and n, inclusive. UML
27
An aggregation relationship is usually represented as a data field in the aggregating class. UML
28
Aggregation may exist between objects of the same class. UML
29
Aggregation may exist between objects of the same class. UML
30
Chapter 10 Immutable objects this keyword Composition Previously
31
Class Abstraction
32
Procedural programming Methods OOP Entities grouping related methods and data OOP
33
Class abstraction means to separate class implementation from the use of the class. The user of the class does not need to know how the class is implemented. Class Abstraction
34
Example: Loan class Class Abstraction TestLoanClass Run Loan
35
Example: BMI Class Abstraction UseBMIClass Run BMI
36
Example: Course Class Abstraction TestCource Course
37
Example: Designing Stack Class Abstraction x Y Z Push Pop
38
Example: Stack Class Abstraction Run TestStackOfIntegers
39
Example: Designing Stack Class Abstraction StackOfIntegers
40
Example: Guess Date Class Abstraction UseGuessDateClass RunGuessDate
41
Class Design Guidelines
42
Coherence A class should describe a single entity All the class operations should support a coherent purpose. You can use a class for students, for example, but you should not combine students and staff in the same class, because students and staff have different entities. Guideline
43
A single entity with too many responsibilities can be broken into several classes to separate responsibilities. Guideline
44
Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. Therefore, you should design a class that imposes no restrictions on what or when the user can do with it. Guideline
45
Design the properties to ensure that the user can set properties in any order, with any combination of values. Design methods to function independently of their order of occurrence. Guideline
46
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible. Guideline
47
Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. Guideline
48
Always place the data declaration before the constructor, and place constructors before methods. Always provide a constructor and initialize variables to avoid programming errors. Guideline
49
Make the fields private and accessor methods public if they are intended for the users of the class. Make the fields or method protected if they are intended for extenders of the class (more on extension and inheritance later). Guideline
50
You can use get methods and set methods to provide users with access to the private data, but only to private data you want the user to see or to modify. A class should also hide methods not intended for client use. Guideline
51
A property (data field) that is shared by all the instances of the class should be declared as a static property. Guideline
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.