1 / 8

Interfaces and an Array List

Interfaces and an Array List. Dan Fleck 2007. Java Interfaces. An interface Specifies methods and constants, but supplies no implementation details Can be used to specify some desired common behavior that may be useful over many different types of objects

rmueller
Download Presentation

Interfaces and an Array List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces and an Array List Dan Fleck 2007

  2. Java Interfaces • An interface • Specifies methods and constants, but supplies no implementation details • Can be used to specify some desired common behavior that may be useful over many different types of objects • The Java API has many predefined interfaces • Example: java.util.Collection (from Carrano slides)

  3. Java Interfaces • A class that implements an interface must • Include an implements clause • Provide implementations of the methods of the interface • To define an interface • Use the keyword interface instead of class in the header • Provide only method specifications and constants in the interface definition (from Carrano slides)

  4. Example Interface public interface AreaStructure { /** Returns the area of this object. */public double getArea(); /** Returns how may square feet this thing is. */public double getSquareFeet(); }

  5. Building class public class Building implements AreaStructure { private int numFloors; private double floorSquareFeet; private int numDoors; public Building(int floors, int doors, double sqFeetPerFloor) { numFloors = floors; floorSquareFeet = sqFeetPerFloor; numDoors = doors; } public int getNumDoors() { return numDoors; } public double getArea() { return floorSquareFeet *numFloors; } public double getSquareFeet() { return floorSquareFeet; } }

  6. Main class public static void main(String args[]) { Building building1 = new Building(10, 2, 1800.0); AreaStructure area1 = new Building(5, 1, 500.0); System.out.println("Building 1 Area is :"+building1.getArea()); System.out.println("Area1 area is:"+area1.getArea()); System.out.println("Building 1 Doors are: "+building1.getNumDoors()); // This does not work because Java treats the Object as a // AreaStructure which does NOT have a getNumDoors() method!!! System.out.println("Area1 doors are:"+area1.getNumDoors());

  7. Now lets create a List interface A List is an ordered group of Objects that you can request by index. Lists allow duplicate objects. public interface List { // Add to the List// Remove from the List// Remove everything from the list// Is the list empty? // Insert into the list// Get an item from the list}

  8. List Example • See example Program in InterfacesAndLists Netbeans project.

More Related