Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: Interfaces and Packages Vyacheslav Grebenyuk CTDE, AI dept., KhNURE.

Similar presentations


Presentation on theme: "Java Programming: Interfaces and Packages Vyacheslav Grebenyuk CTDE, AI dept., KhNURE."— Presentation transcript:

1 Java Programming: Interfaces and Packages Vyacheslav Grebenyuk CTDE, AI dept., KhNURE

2 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20062 Content Creating and Using Interfaces Creating and Using Packages

3 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20063 What is interface An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy it also defines a set of methods but does not implement them A class that implements the interface agrees to implement all the methods defined in the interface Definition: An interface is a named collection of method definitions, without implementations.

4 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20064 How an interface differs from an abstract class 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

5 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20065 Defining an Interface The Interface Declaration

6 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20066 The Interface Body contains method declarations for all the methods included in the interface All methods declared in an interface are implicitly public and abstract An interface can contain constant declarations in addition to method declarations ( public, static, and final )

7 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20067 Implementing an Interface By Convention: The implements clause follows the extends clause, if it exists public class StockApplet extends Applet implements StockWatcher {

8 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20068 Using an Interface as a Type public class StockMonitor { public void watchStock(StockWatcher watcher, TickerSymbol tickerSymbol, BigDecimal delta) {... }

9 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 20069 Warning! Interfaces Cannot Grow public interface StockWatcher { void valueChanged(TickerSymbol tickerSymbol, BigDecimal newValue); void currentValue(TickerSymbol tickerSymbol, BigDecimal newValue); } Instead do next: public interface StockTracker extends StockWatcher { void currentValue(TickerSymbol tickerSymbol, BigDecimal newValue); }

10 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200610 Creating and Using Packages 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

11 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200611 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 {... }

12 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200612 Reasons for bundling classes in a package 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

13 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200613 Creating a Package To create a package, put a type (class, interface, enum, or annotation) in it To do this, put a package statement at the top of the source file in which the type is defined Remember! Only public package members are accessible from outside of the package

14 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200614 Naming a Package By Convention: Companies use their reversed Internet domain name in their package names (for example, com.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, com.company.region.package)

15 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200615 Naming a Package Domain namePackage name clipart-open.orgorg.clipart_open free.fonts.intint_.fonts.free poetry.7days.comcom._7days.poetry

16 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200616 Using Package Members Refer to the member by its long (qualified) name Import the package member Import the member's entire package

17 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200617 Managing Source and Class Files class name graphics.Rectangle pathname to file graphics\Rectangle.java

18 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200618 Classes files Like a.java file, a.class file should also be in a series of directories that reflect the package name However, it does not have to be in the same directory as its source

19 (С) ЦТДО, каф. Искусственного интеллекта, ХНУРЭ, 200619 Class path 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.


Download ppt "Java Programming: Interfaces and Packages Vyacheslav Grebenyuk CTDE, AI dept., KhNURE."

Similar presentations


Ads by Google