Interfaces and Packages - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Interfaces and Packages

Description:

{ package graphics; public class Rectangle extends Graphic implements Draggable ... Convention: use reversed Internet domain name for package names ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 18
Provided by: ayse7
Category:

less

Transcript and Presenter's Notes

Title: Interfaces and Packages


1
Lesson 4
  • Interfaces and Packages

2
Interfaces
  • Definition An interface is a named collection of
    method definitions (without implementations). An
    interface can also declare constants.
  • An interface defines a protocol of behavior that
    can be implemented by any class anywhere in the
    class hierarchy.
  • An interface 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, thereby agreeing to certain behavior.

3
Interfaces vs. Abstract Classes
  • 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.

4
Defining an Interface
5
Implementing an Interface
  • When a class implements an interface, it agrees
    to define all methods of that interface. A class
    can implement many different interfaces.
  • public class StockApplet extends Applet
    implements StockWatcher
  • ...
  • public void valueChanged(String tickerSymbol,
    double newValue)
  • if (tickerSymbol.equals(sunTicker)) ...
  • else if (tickerSymbol.equals(oracleTicker))
    ...
  • else if (tickerSymbol.equals(ciscoTicker))
    ...

6
Creating and Using Packages
  • A package is a collection of related classes and
    interfaces providing access protection and
    namespace management.

7
Creating and Using Packages
  • //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
  • . . .

8
Creating a Package
  • package graphics
  • public class Circle extends Graphic implements
    Draggable
  • . . .
  • package graphics
  • public class Rectangle extends Graphic implements
    Draggable
  • . . .

9
Naming a Package
  • graphics.Rectangle
  • java.awt.Rectangle
  • Convention use reversed Internet domain name for
    package names
  • E.g., yeditepe.cse.cse252.graphics

10
Using Package Members
  • Referring to a Package Member by Name
  • graphics.Rectangle myRect new
    graphics.Rectangle()
  • Importing a Package Member
  • import graphics.Circle
  • Circle myCircle new Circle()
  • Importing an Entire Package
  • import graphics.
  • Circle myCircle new Circle()
  • Rectangle myRectangle new Rectangle()

11
Managing Source and Class Files
class namegraphics.Rectangle pathname to
filegraphics/Rectangle.java
12
Managing Source and Class Files
13
Managing Source and Class Files
14
Class Path
  • A class path is an ordered list of directories or
    ZIP files in which to search for class files.
  • By default, the compiler and the interpreter
    search the current directory and the ZIP file
    containing the Java platform class files.

15
Vector Class
  • The Vector class implements a growable array of
    objects.
  • Like an array, it contains components that can be
    accessed using an integer index.
  • However, the size of a Vector can grow or shrink
    as needed to accommodate adding and removing
    items after the Vector has been created.

16
Vectors
  • Creation
  • Vector myVector new Vector () // or
  • Vector myVector new Vector(50)
  • Insertion
  • for (int i 1 i lt 20 i)
  • myVector.addElement( new String ("I am string "
    i))
  • Access
  • System.out.println("Element"
    myVector.elementAt(5) )
  • Traversing
  • Enumeration e myVector.elements()
  • for (e.hasMoreElements())
  • String myString (String) e.nextElement()
    System.out.println(myString)

17
Lab Work
  • Open and read a text file.
  • Find the words in the text file. Assume that
    words are separated by spaces.
  • Sort and print the words.
  • Use Vector class to store numbers.
Write a Comment
User Comments (0)
About PowerShow.com