Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single...
Transcript of Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single...
![Page 1: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/1.jpg)
Encapsulation
CMSC 202
![Page 2: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/2.jpg)
Types of Programmers
• Class programmers
– Developers of new classes
– Goal: Expose the minimum interface necessary to use a new class
– Implementation (code) is “hidden”
• Client programmers
– Write code (the client program) that uses existing classes
2 Version 9/12
![Page 3: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/3.jpg)
Encapsulation
• Combining data and operations into a single entity (class)
• Providing appropriate access control
• Achieved through information hiding/abstraction
3
Client Program
Pre-coded Class X
Pre-coded Class Y
Version 9/12
Class X interface
Class Y interface
![Page 4: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/4.jpg)
The Value of Encapsulation
• Client programmers do not need to know how the class is implemented, only how to use it.
• Class implementation may be changed with no impact on code that uses the class.
4 Version 9/12
Client Program
Pre-coded Class X Class X interface
Modified Class X
Class X interface
X
![Page 5: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/5.jpg)
Class Interface
• Instance variables
– name
– what it represents
– type
• Methods
– name
– what it does (not how)
– pre & post-conditions
– parameters
– return value Version 9/12 5
public String make; // car make public String model; // car model public int year; // car year
/* Resets a car’s make * Preconditions: New make is non-null * Postconditions: Car’s make is reset * Parameters: Car’s new make * Returns: Nothing */ public void getMake(String newMake )
![Page 6: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/6.jpg)
Visibility Modifiers
• Provide access control to class
– instance variables
– constants
– methods
• public – directly accessible by client code
• private – accessible only by methods within the class (class scope)
• Other – later
6 Version 9/12
Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
![Page 7: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/7.jpg)
Private Instance Variables Example
7
public class Car {
public String model;
public String make;
private int year;
public void setYear(int newYear){
year = newYear;
}
}
Version 9/12
public class CarDemo {
public static void main(String[] args) {
Car car = new Car();
car.make = “Toyota"; // OK
car.model = “Prius"; // OK
car.year = 2008; // compiler error
car.setYear(2008); // OK
}
}
![Page 8: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/8.jpg)
Private Instance Variables: Summary
• Only accessible within the class
• Are not directly accessible by a client program
• Hide implementation details, promoting encapsulation
• Good programming practice: – Label all instance variables as non-public (typically,
private).
• Bottom line: – The class should have complete control over
how/when/if the instance variables are changed or accessed.
8 Version 9/12
Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
![Page 9: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/9.jpg)
Accessors and Mutators
• Accessor method (getter) – Retrieves the value of a private instance variable – Conventions:
• Start the method name with get. • Start edge case for booleans with is.
• Mutator method (setter) – Changes the value of a private instance variable. – Convention: Start the method name with set.
• Gives the client program indirect access to the instance variables.
9 Version 9/12
![Page 10: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/10.jpg)
Why Accessors and Mutators?
• Accessors
– The class implementer decides • which instance variables will have accessors, and
• in what form they will be returned.
• Mutators
– validate the new value of the instance variable, and
– decide whether or not to actually make the requested change.
10 Version 9/12
![Page 11: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/11.jpg)
Example Car Accessor and Mutator
11
public class Car {
private int year; // 4-digit year between 1000 and 9999
private String vin; // Vehicle identification number
// Accessor to return the vin member
public String getVin() {
return vin;
}
// Mutator to change the year member
public boolean setYear(int newYear) {
if(newYear >= 1000 && newYear <= 9999) {
year = newYear;
return true;
}
return false;
}
// ...
} Version 9/12
![Page 12: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/12.jpg)
Accessor/Mutator Caution
• In general, do not provide accessors and mutators for all private instance variables.
• Bottom line: Limit the class interface.
12 Version 9/12
![Page 13: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/13.jpg)
Private Methods
• Have class scope
• Cannot be invoked by a client program
• Used as “helper” methods to support top-down implementation of a public method.
13 Version 9/12
Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
![Page 14: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/14.jpg)
Private Method Example
14
public class Car {
private int year; // 4-digit year between 1000 and 9999
// Helper method – class scope
private boolean isValidYear(int year) {
return(year >= 1000 && year <= 9999);
}
// Mutator to change the year member
public boolean setYear(int newYear) {
if(isValidYear(newYear)) {
year = newYear;
return true;
}
return false;
}
// ...
} Version 9/12
![Page 15: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/15.jpg)
Encapsulation Summary
• Combine methods and data in a single class.
• Use private instance variables for information hiding.
• Minimize the class’s public interface.
15
Keep it secret, keep it safe
Version 9/12
![Page 16: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/16.jpg)
Method Overloading
![Page 17: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/17.jpg)
Method Names
• Different classes can define a method with the same name.
• Example:
• Java can determine which method to call based on the type of the calling object.
17
Cat myCat = new Cat();
Dog myDog = new Dog();
System.out.println(myCat);
System.out.println(myDog);
Version 9/12
![Page 18: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/18.jpg)
Method Overloading
• When two or more methods in the same class have the same name
• Example:
18 Version 9/12
public boolean setStyle(String make, String model)
public boolean setStyle(String make)
Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
![Page 19: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/19.jpg)
Car Class — Overloaded setStyle
19
public class Car {
// ...
private String model;
private String make;
public boolean setStyle(String make, String model) {
if(isValidMake(make) && isValidModel(model)) {
this.make = make;
this.model = model;
return true;
}
return false;
}
public boolean setStyle(String make) {
if(isValidMake(make)) {
this.make = make;
return true;
}
return false;
}
private boolean isValidMake(String make) {
return make != null && !make.equals("");
}
private boolean isValidModel(String model) {
return model != null && !model.equals("");
}
} Version 9/12
![Page 20: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/20.jpg)
CarDemo Class
Version 9/12 20
How does Java know which setStyle method to call?
public class CarDemo {
public static void main(String[] args) {
Car car = new Car();
car.setStyle("Mazda");
System.out.println(car);
car.setStyle("Audi", "A8");
System.out.println(car);
}
}
![Page 21: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/21.jpg)
Method Signature
• A method is uniquely identified by its
– name and
– parameter list (types and order).
• This is known as its signature.
• Examples …
21
public boolean setStyle(String make, String model)
public boolean setStyle(int year, String make, String model)
public boolean setStyle(String make, String model, String color)
public boolean setStyle(int year, String color)
public boolean setStyle(String make)
Version 9/12
Reference: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
![Page 22: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/22.jpg)
Return Type is Not Enough
• Attempt to overload Car’s setStyle() method by using different return types:
• This is NOT valid method overloading. • The method return value can be ignored.
22
public void setStyle(String make) { /* code here */ }
public boolean setStyle(String model) { /* code here */ }
car.setStyle("Mazda");
Version 9/12
![Page 23: Encapsulation - csee.umbc.edu · Encapsulation •Combining data and operations into a single entity (class) •Providing appropriate access control •Achieved through information](https://reader033.fdocuments.net/reader033/viewer/2022051917/6009b09f11c49f129a47867a/html5/thumbnails/23.jpg)
Common Problem • Type coercion can confuse the compiler:
• So now,
• The Java compiler can’t decide whether to – promote 7 to 7.0 and call the first version of calculateAverage, or – promote 5 to 5.0 and call the second.
• Solution: Cast the arguments so that they match one of the signatures.
23
//version 1
public double calculateAverage(int a, double b) { /* code */ }
//version 2
public double calculateAverage(double a, int b) { /* code */ }
MathUtils math = new MathUtils();
math.calculateAverage(5, 7); // two int parameters
Version 9/12
math.calculateAverage(5, (double)7); OR
Math.calculateAverage((double)5, 7);