Chapter 03
Chapter 03
Chapter 3
Chapter Contents
Encapsulation
Specifying Methods
Java Interfaces
• Writing an Interface
• Implementing an Interface
• An Interface as a Data Type
• Type Casts Within an Interface Implementation
• Extending an Interface
• Named Constants Within an Interface
• Interfaces Versus Abstract Classes
Choosing Classes
• Identifying Classes
• CRC Cards
Reusing Classes 2
Encapsulation
Hides the fine detail of the inner workings of
the class
• The implementation is hidden
• Often called "information hiding"
6
Specifying Methods
Specify what each method does
Precondition
• Defines responsibility of client code
Postcondition
• Specifies what will happen if the preconditions
are met
Assertions can be written as comments to
identify design logic
// Assertion: intVal >= 0
7
Java Interface
A program component that contains
• Public constants
• Signatures for public methods
• Comments that describe them
Begins like a class definition
• Use the word interface instead of class
10
Implementing an Interface
13
Named Constants Within an Interface
14
Interfaces Versus Abstract Classes
Purpose of interface similar to purpose of
abstract class
But … an interface is not a base class
• It is not a class of any kind
16
Choosing Classes
19
CRC Cards
Index cards – each card represents one
class
Write a descriptive name for class at top
List the class's responsibilities
• The methods
Indicate interactions
• The collaborations
21
Unified Modeling Language
Used to illustrate a system's classes and
relationships
Provides a class diagram
• Class name
• Attributes
• Operations
24
Reusing Classes
Much software combines:
• Existing components
• New components
25