Title: Lesson : Interfaces and Packages
1Lesson Interfaces and Packages
- 1. Creating Interfaces
- 2. Creating and Using Packages
21. Creating Interfaces
- 1. What Is an Interface?
- 2. Defining an Interface
- 3. Implementing the Sleeper Interface
- 4. Using an Interface as a Type
- 5. Warning! Interface Cannot Grow
3What Is an Interface?
- Interface
- A named collection of method definitions (without
implementations) that can include constant
declarations. - Use to define a protocol of behavior that can be
implemented by any class anywhere in the class
hierarchy. - Example
- How to create and use an interface.
- Why you need them.
- How to decide when to use an interface versus
when to use a class.
4Example AlarmClock and Sleeper
- The AlarmClock class
- A service provider
- Notifies objects after a certain amount of time
has elapsed. - To get on AlarmClock's list of sleepers
- Ask the alarm clock to wake it up.
- letMeSleepFor method
- Implement the wakeUp method.
- Sleeper
- GUIClock
5Why Use Interfaces?
- Java doesn't support multiple inheritance
- Hence, you use an interface instead.
- Interface and multiple class inheritance are
quite different - A class inherits only constants from an
interface. - A class cannot inherit method implementations
from an interface. - The interface hierarchy is independent of the
class hierarchy. - Java does allow multiple interface inheritance
6Usefulness of Interfaces
- Capturing similarities between unrelated classes
without artificially forcing a class
relationship. - Declaring methods that one or more classes are
expected to implement. - Revealing an object's programming interface
without revealing its class.
7Defining an Interface
- An interface definition has two components
- The interface declaration.
- Declares various attributes about the interface.
- The interface body.
- Contains the constant and method declarations
within that interface.
8The Interface Declaration
- An interface can extend other interfaces just as
a class. - An interface can extend any number of
interfaces. - An interface inherits all constants and methods
from its superinterfaces.
9The Interface Body
- The method declaration
- It is not implemented.
- The constant declaration
- Classes that implement an interface can treat the
constants as though they were inherited. - Any class can use an interface's constants from
the name of the interface.
10Implementing the Interface
- To declare a class that implements an interface,
include an implements clause in the class
declaration. - A class can implement more than one interface .
- The implements keyword is followed by a
comma-separated list of the interfaces
implemented by the class. - The class must provide method implementations for
all of the methods declared in the interface and
its superinterfaces.
11Using an Interface as a Type
- Defining a new interface
- Defining a new reference data type.
- For example(the AlarmClock class)
- To declare an array of Sleepers
- private Sleeper sleepers new
SleeperMAX_CAPACITY - As an argument to letMeSleepFor
- private boolean letMeSleepFor(Sleeper theSleeper,
int time) -
- . . .
12Interfaces Cannot Grow
- Changing Interface
- If you make this change to Sleeper, all classes
that implement the old Sleeper will break because
they don't implement the interface anymore! - To create more interfaces later or to break your
customer's code.
public interface Sleeper public void
wakeUp() public void beep() public long
ONE_SECOND 1000
132. Creating and Using Packages
- 1. Creating a Package
- 2. Using Package Members
- 3. Managing Source and Class Files
14Creating and Using Packages
- A package(definition)
- A collection of related classes and interfaces
that provides access protection and namespace
management. - A package used to
- to make classes easier to find and use.
- to avoid naming conflicts.
- to control access.
- The case of JDK
- Applet classes are in java.applet
- I/O classes are in java.io
- GUI widget classes are in java.awt.
15Several Reasons
- Easy to determine that these classes and
interfaces are related. - Easy to know where to find classes and interfaces
that provide graphics-related functions. - Avoid conflict with class names in other
packages. - You can allow classes within the package to have
unrestricted access to each other.
16Creating a Package
- A package statement
- At the top of the source file in which the class
or interface is defined - package graphics
- public class Circle extends Graphic implements
Draggable - . . .
-
- The Circle class is a public member of the
graphics package. - Recommend that you use the one-public-class-per-fi
le convention
17Naming a Package
- If the classes have same name and are in
different packages, then the compiler allows
both classes. - By Convention
- Companies use their reversed Internet domain name
in their package names, like this - com.company.package.
- com.company.region.package.
18Using Package Members
- Only public package members are accessible
outside the package. - To use a public package member,
- refer to the member by its long (disambiguated)
name, - import the package member, or
- import the member's entire package.
19Referring to a Package Member by Name
- Short names
- The name specified in their declaration
- If the code you are writing is in the same
package as that member. - If the member's package has been imported.
- Long name
- The name that includes the package name.
- If you want to use a member from a different
package. - That package has not been imported.
- graphics.Rectangle
20Importing a Package Member
- To import a specific member into the current
file, put an import statement. - import graphics.Circle
- Circle myCircle new Circle()
- This approach works just fine if you use just a
few members from the graphics package.
21Importing an Entire Package
- To import all of the classes and interfaces
contained in a particular package, use the import
statement with the asterisk (). - import graphics.
- Circle myCircle new Circle()
- Rectangle myRectangle new Rectangle()
- Automatically imported packages
- The default package (the package with no name)
- The java.lang package
- The current package
22Disambiguating a Name
- Two packages have classes that the name is equal
and both packages are imported, you must refer to
the member by its long name. - Example
- The graphics and java.awt packages contains a
Rectangle class. - Both graphics and java.awt have been imported.
- Rectangle rect
- graphics.Rectangle rect
23Managing Source and Class Files
- Save the source code
- The short name of the class or interface
- The extension is .java
- Put the source file in a directory
- Directory name reflects the packages name
- graphics.Rectangle //class name
- graphics/Rectangle.java //pathname to file
24Managing Source and Class Files...
- A class path is a list of directories or zip
files to search for class files. - The basename of the output file
- The name of the class or interface
- its extension is .class
- A .class file should also be in a series of
directories that reflect the package name. - The current directory and the JDK class files are
automatically in your class path.