Java Programming Interfaces and Packages Vyacheslav Grebenyuk CTDE
Java Programming: Interfaces and Packages Vyacheslav Grebenyuk CTDE, AI dept. , Kh. NURE (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006
Content Creating and Using Interfaces n Creating and Using Packages n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 2
What is interface An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy n it also defines a set of methods but does not implement them n A class that implements the interface agrees to implement all the methods defined in the interface n Definition: An interface is a named collection of method definitions, without implementations. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 3
How an interface differs from an abstract class n n An interface cannot implement any methods, whereas an abstract class can A class can implement many interfaces but can have only one superclass An interface is not part of the class hierarchy Unrelated classes can implement the same interface (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 4
Defining an Interface n The Interface Declaration (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 5
The Interface Body contains method declarations for all the methods included in the interface n All methods declared in an interface are implicitly public and abstract n An interface can contain constant declarations in addition to method declarations (public, static, and final) n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 6
Implementing an Interface n By Convention: The implements clause follows the extends clause, if it exists public class Stock. Applet extends Applet implements Stock. Watcher { (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 7
Using an Interface as a Type public class Stock. Monitor { public void watch. Stock(Stock. Watcher watcher, Ticker. Symbol ticker. Symbol, Big. Decimal delta) {. . . } } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 8
Warning! Interfaces Cannot Grow public interface Stock. Watcher { void value. Changed(Ticker. Symbol ticker. Symbol, Big. Decimal new. Value); void current. Value(Ticker. Symbol ticker. Symbol, Big. Decimal new. Value); } n Instead do next: public interface Stock. Tracker extends Stock. Watcher { void current. Value(Ticker. Symbol ticker. Symbol, Big. Decimal new. Value); } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 9
Creating and Using Packages n To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages Definition: A package is a collection of related types providing access protection and name space management. Note that types refers to classes, interfaces, enums, and annotations (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 10
Imaginable application //in the Graphic. java file public abstract class Graphic {. . . } //in the Circle. java file public class Circle extends Graphic implements Draggable {. . . } //in the Rectangle. java file public class Rectangle extends Graphic implements Draggable {. . . } //in the Draggable. java file public interface Draggable {. . . } (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 11
Reasons for bundling classes in a package n n You and other programmers can easily determine that these types are related You and other programmers know where to find types that can provide graphics-related functions The names of your types won't conflict with the type names in other packages because the package creates a new namespace You can allow types within the package to have unrestricted access to one another yet still restrict access for types outside the package (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 12
Creating a Package To create a package, put a type (class, interface, enum, or annotation) in it n To do this, put a package statement at the top of the source file in which the type is defined n Remember! Only public package members are accessible from outside of the package n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 13
Naming a Package n By Convention: Companies use their reversed Internet domain name in their package names (for example, company. package). Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, company. region. package) (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 14
Naming a Package Domain name Package name clipart-open. org. clipart_open free. fonts. int_. fonts. free poetry. 7 days. com. _7 days. poetry (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 15
Using Package Members Refer to the member by its long (qualified) name n Import the package member n Import the member's entire package n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 16
Managing Source and Class Files class name n pathname to file n graphics. Rectangle graphicsRectangle. java (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 17
Classes files Like a. java file, a. class file should also be in a series of directories that reflect the package name n However, it does not have to be in the same directory as its source n (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 18
Class path n Both the compiler and the JVM search for classes in each directory or JAR file listed in your class path Definition: A class path is an ordered list of directories or JAR files in which to search for class files. (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 2006 19
- Slides: 19