Vous êtes sur la page 1sur 20

Module 5

Creating a Class: Using Encapsulation

Java Programming Language

Objectives
Understand the concept of encapsulation Implement encapsulation in the Java language Use the static keyword Examine static imports

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 2 of 20

Examining the Concept of Encapsulation


Unencapsulated data
day:int price:double year:int name:String symbol:String month:int

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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)

Verify days in month

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 6 of 20

The Benets of Encapsulation


Protecting data integrity Application maintainability

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 7 of 20

Implementing Encapsulation in Java Technology


The package statement: A class in a package is visible and therefore accessible to all other classes in the same package. A class marked public is visible to classes in other packages. A class not marked public is hidden to classes in other packages. The class statement encapsulates attributes, constructors, and methods into a single unit that can be compiled.

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 8 of 20

Implementing Encapsulation in Java Technology


Access modifiers: Private Default Protected Public
Modier
private default protected public

Same Class
Yes Yes Yes Yes

Same Package
Yes Yes Yes

Subclass

Universe

Yes Yes Yes

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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) { } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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) { } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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) { } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 12 of 20

Using the static Keyword


Class attributes Class methods

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Class Attributes
Accessing a static field
1 2 3 4 5 public class OtherClass { public void incrementNumber() { Count.counter++; } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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; }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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()); } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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! } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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); } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

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; } }

Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision G

Module 5, slide 20 of 20

Vous aimerez peut-être aussi