Académique Documents
Professionnel Documents
Culture Documents
Objectives
Understand the concept of encapsulation Implement encapsulation in the Java language Use the static keyword Examine static imports
Module 5, slide 2 of 20
Module 5, slide 3 of 20
Encapsulation Steps
Encapsulation Step 1: Group-Related Data
MyDate
day : int month : int year : int
Stock
symbol : String name : String price : double
Module 5, slide 4 of 20
Encapsulation Steps
Group Data With Behavior
MyDate
day : int month : int year : int getDay() : int getMonth() : int getYear() : int setDay(int) : boolean setMonth(int) : boolean setYear(int) : boolean
Stock
symbol : String name : String price : double Stock(symbol : String) getSymbol() : String getName() : String getPrice() : double setName() setPrice(double)
Module 5, slide 5 of 20
Encapsulation Steps
Implement Access Control
MyDate
-day : int -month : int -year : int +getDay() +getMonth() +getYear() +setDay(int) : boolean +setMonth(int) : boolean +setYear(int) : boolean -symbol : String -name : String -price : double +Stock(symbol : String) +getSymbol() : String +getName() : String +getPrice() : double +setName() +setPrice(double)
+ symbol represents external or public access - symbol represents internal or private access
Module 5, slide 6 of 20
Module 5, slide 7 of 20
Module 5, slide 8 of 20
Same Class
Yes Yes Yes Yes
Same Package
Yes Yes Yes
Subclass
Universe
Module 5, slide 9 of 20
Encapsulation Examples
1 2 3 4 5 6 7 8 9 10 package com.abc.util; public class Date { private int day; public Date() {//... } public void addDays(int days) { } int getDaysInMonth(int month) { } }
Module 5, slide 10 of 20
Encapsulation Examples
1 2 3 4 5 6 7 8 9 package com.abc.brokerage; public class Stock { private String symbol; public Stock(String symbol, double price) { } public String getSymbol() { } public void setSymbol(String symbol) { } }
Module 5, slide 11 of 20
Encapsulation Examples
1 2 3 4 5 6 7 8 9 package com.abc.brokerage; import abc.util.Date; class StockAnalyzer { private MyDate date; double sell(Stock stock, int quantity) { } public double buy(Stock stock, int quantity) { } }
Module 5, slide 12 of 20
Module 5, slide 13 of 20
Class Attributes
Count
+counter : int = 0 -serialNumber : int
instanceOf instanceOf
c1 : Count
serialNumber=1
c2 : Count
serialNumber=2
1 2 3 4 5 6 7 8 9
public class Count { private int serialNumber; public static int counter = 0; public Count() { counter++; serialNumber = counter; } }
Module 5, slide 14 of 20
Class Attributes
Accessing a static field
1 2 3 4 5 public class OtherClass { public void incrementNumber() { Count.counter++; } }
Module 5, slide 15 of 20
Class Methods
Class method example
1 2 3 4 5 6 7 8 9 10 11 12 public class Count2 { private int serialNumber; private static int counter = 0; public static int getTotalCount() { return counter; } public Count2() { counter++; serialNumber = counter; }
Module 5, slide 16 of 20
Class Methods
Accessing a class method
1 2 3 4 5 6 7 8 9 public class TestCounter { public static void main(String[] args) { System.out.println("Number of counter is " + Count2.getTotalCount()); Count2 count1 = new Count2(); System.out.println("Number of counter is " + Count2.getTotalCount()); } }
Module 5, slide 17 of 20
Class Methods
Missing instance reference variable
1 2 3 4 5 6 7 8 public class Count3 { private int serialNumber; private static int counter = 0; public static int getSerialNumber() { return serialNumber; // COMPILER ERROR! } }
Module 5, slide 18 of 20
Static Initializers
Static blocks execute only once Static blocks execute in the order of their appearance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import com.mycompany.utilities.Database.*; public class Count4 { public static int counter; static { if (Database.tableExists(?COUNTER?)) { counter = Database.getSingleField('COUNTER'); } } } public class TestStaticInit { public static void main(String[] args) { System.out.println("counter = "+ Count4.counter); } }
Module 5, slide 19 of 20
Static Imports
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.myproject.shapes; static import Math.PI: public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double area() { return PI*radius*radius; } public double circumferance { return 2*PI*radius; } }
Module 5, slide 20 of 20