interfaces - Interfaces Inner Classes and Namespaces...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Interfaces, Inner Classes, and Namespaces Interfaces An interface in Java is like a class that hasn’t been implemented yet. An interface contains a list of method headers, none of which are implemented. It is a way to list the features you want in a particular class without yet deciding how to implement them. Interfaces are very useful with data structures because we can list the functionality we’d like the data structure to have, and then implement them in several different ways. Declaring an Interface Here’s a sample interface: public interface ArrayCollection { void add(Object elem); void print(); } This interface should be stored in the file ArrayCollection.java . This interface defines what functionality we’d like a collection of general elements to have. We don’t know exactly how we’re going to implement this collection, but we do know that we want to be able to add elements and print elements. Notice that we do not include visibility modifiers in an interface (they are all public). Implementing an Interface Of course, we do eventually have to write code for array collections, or we won’t be able to use the interface. To do this, we write a class that implements the ArrayCollection interface. If a class implements an interface, then it must implement every method defined in the interface. The method headers in the class must look EXACTLY like the method headers in the interface. First, we’ll implement the ArrayCollection interface using the by storing all elements added in an array (up to the maximum size): public class ArrayList implements ArrayCollection { private Object[] arr; private int size; public ArrayList(int max) { arr = new Object[max]; size = 0; } public void add(Object elem) {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
//Don’t add if we’ve reached our max size if (size == arr.length) return; arr[size] = elem; size++; } public void print() { for (int i = 0; i < size; i++) { System.out.println(arr[i].toString()); } } } Similarly, we can implement the ArrayCollection interface using by only storing each unique element added (up to the maximum size): public class ArraySet implements ArrayCollection { private Object[] arr; private int size; public ArraySet(int max) { arr = new Object[max]; size = 0; } public void add(Object elem) { //Don’t add if we’ve reached our max size
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2010 for the course CIS 200 taught by Professor Staff during the Spring '08 term at Kansas State University.

Page1 / 7

interfaces - Interfaces Inner Classes and Namespaces...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online