Lecture 2
Lecture 2
Programming II
Lecture 2: Encapsulation
Encapsulation
• encapsulation: Hiding implementation details from clients.
– Encapsulation forces abstraction.
• separates external view (behavior) from internal view (state)
• protects the integrity of an object's data
Private fields
A field that cannot be accessed from outside the class
– Examples:
private int id;
private String name;
• Inside setLocation,
– To refer to the data field x, say this.x
– To refer to the parameter x, say x
Calling another constructor
public class Point {
private int x;
private int y;
public Point() {
this(0, 0); // calls (x, y) constructor
}
...
}
• Advantages:
– code reuse
– splits up the program logic into manageable chunks
Main Class #1
main
method1
method2
Class #2 Class #3
method3 method4
method5 method6
Example (Static methods)
// This program sees whether some interesting numbers are prime.
public class Primes1 {
public static void main(String[] args) {
int[] nums = {1234517, 859501, 53, 142};
for (int i = 0; i < nums.length; i++) {
if (isPrime(nums[i])) {
System.out.println(nums[i] + " is prime");
}
}
}
// Returns the number of factors of the given integer.
public static int countFactors(int number) {
int count = 0;
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
count++; // i is a factor of the number
}
}
return count;
}
// Returns true if the given number is prime.
public static boolean isPrime(int number) {
return countFactors(number) == 2;
}
}
Classes as modules
• module: A reusable piece of software, stored as a class.
– Example module classes: Math, Arrays, System
// This class is a module that contains useful methods
// related to factors and prime numbers.
public class Factors {
// Returns the number of factors of the given integer.
public static int countFactors(int number) {
int count = 0;
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
count++; // i is a factor of the number
}
}
return count;
}
// Returns true if the given number is prime.
public static boolean isPrime(int number) {
return countFactors(number) == 2;
}
}
More about modules
• A module is a partial program, not a complete program.
– It does not have a main. You don't run it directly.
– Modules are meant to be utilized by other client classes.
• Syntax:
class.method(parameters);
• Example:
int factorsOf24 = Factors.countFactors(24);
Using a module
// This program sees whether some interesting numbers are prime.
public class Primes {
public static void main(String[] args) {
int[] nums = {1234517, 859501, 53, 142};
for (int i = 0; i < nums.length; i++) {
if (Factors.isPrime(nums[i])) {
System.out.println(nums[i] + " is prime");
}
}
}
}
// This program prints all prime numbers up to a given maximum.
public class Primes2 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Max number? ");
int max = console.nextInt();
for (int i = 2; i <= max; i++) {
if (Factors.isPrime(i)) {
System.out.print(i + " ");
} }
System.out.println();
}
}
Modules in Java libraries
// Java's built in Math class is a module
public class Math {
public static final double PI =
3.14159265358979323846;
...
– Example:
private static int theAnswer = 42;
public BankAccount() {
objectCount++; // advance the id, and
id = objectCount; // give number to account
}
...
public int getID() { // return this account's id
return id;
}
}
Static methods
// the same syntax you've already used
for methods
public static type name(parameters) {
statements;
}
24
Defining a Class as part of a Package
• Include “package” keyword as the first statement in the class
followed by the package name
• Example:
package myPackage ;
This specifies that the class is part of package myPackage (the class must be
stored in a folder called myPackage)
• If you omit the package definition from the class the class is put in
the default package
• You can create a hierarchy of packages
• Example:
package
java2.lec3.myPakage ;
25
Accessing Classes in a Package
• To bring a class in certain package to visibility in your class you need to use the
“import” keyword
• Example:
import java2.lec3.myPakage.Stack ;
• You could also import the entire package in one step using *
• Example:
import java2.lec3.myPakage.* ;
(note: import this might negatively affect program compilation time)
• You may also access a class in certain package directly by specifying its package
as a prefix
• Example:
java2.lec3.myPakage.Stack s1 ;
26
Access Control
Access to member of a class can be defined as follows:
Specifier public Default (No private
Access from specifier)
Same Class √ √ √
Same Package √ √ X
Different Package √ X X
27