Using interfaces in Java.
The HTML file for the applet:

<APPLET CODE="TestInterface.class" HEIGHT=400 WIDTH=400></APPLET>
The interface file:

import java.awt.*;

public interface ColorObject {
    // returns the colors of the object
    public Color [] getColors();

    // returns true if the color is present, false otherwise
    public boolean hasColor(Color c);

    public void draw(Graphics g);
A class that implements the interface:

import java.awt.*;

public class ColorSquare implements ColorObject {
    private Color c;
    private int x;
    private int y;
    private int side;

    public ColorSquare(Color c, int x, int y, int side) {
	this.c = c;
	this.x = x;
	this.y = y;
	this.side = side;

    public Color [] getColors() {
	Color [] cc = {c};
	return cc;

    public boolean hasColor(Color c) {
	return this.c == c;

    public void draw(Graphics g) {

The test file:

import java.awt.*;
import java.applet.*;

public class TestInterface extends Applet {

    public void paint(Graphics g) {
	ColorObject [] co = new ColorObject[4];
	co[0] = new ColorSquare(, 0, 0, 100);

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.