AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Implementing Interfaces
  • Comparable Interface

Was this helpful?

  1. Classes and Object-Oriented Programming

Interfaces

Interfaces in Java allow us to create a collection of methods to guarantee a class's interaction. An interface does not contain method implementation, constructors, or instance variables.

Interfaces are commonly used to help achieve polymorphism.

Implementing Interfaces

To implement an interface we use the implements keyword after our class name, like: public class Dog implements Animal. In this case Dog is our class name, where Animal is our interface.

Let's look at an interface for using a computer. We will call it UseComputer.

public interface UseComputer
{
    /* Notice how we end our method signatures with semi-colons `;`
     * and not curly brackets
     */ 
    public void pressKey(Key keyPressed);
    public void clickMouse(MouseEvent mouse);
    public void shutDown();
    public void startUp();
}

Now, let's take a look at an example class which implements our interface:

public class GeneralUser implements UseComputer 
{
    private boolean isShuttingDown = false;
    private boolean isStartingUp = false;

    public void pressKey(Key keyPressed)
    {
        return keyPressed;
    }

    public void clickMouse(MouseEvent mouse)
    {
        return mouse;
    }

    public void shutDown()
    {
        isShuttingDown = true;
        isStartingUp = false;
    }

    public void startUp()
    {
        isShuttingDown = false;
        isStartingUp = true;
    }
}

As you can see, we store the method signatures in our UseComputer interface, and implement them within our GeneralUser class.

Comparable Interface

Java is full of interfaces that you are free to utilize within your code. One interface you should become familiar with is the Comparable interface. The Comparable interface can be incredibly useful with sorting algorithms.

Here is what an implementation of the Comparable interface looks like:

// Marbles class, implements Comparable
public class Marbles implements Comparable<Marbles>
{
    private String owner;
    private int quantity;

    public Marbles(String ownerName, int mQuantity)
    {
        owner = ownerName;
        quantity = mQuantity;
    }

    public String getOwner()
    {
        return owner;
    }

    public int getQuantity()
    {
        return quantity;
    }

    public void setOwner(String newOwner)
    {
        owner = newOwner;
    }

    public void setQuantity(int newQuantity)
    {
        quantity = newQuantity;
    }

    public int compareTo(Marbles comMarbles)
    {
        int comQuantity = comMarbles.getQuantity();

        return getQuantity() - comQuantity;
    }
}

// Our main class
public class MarblesProgram extends ConsoleProgram 
{
    public void run()
    {
        Marbles marbles1 = new Marbles("Wezley", 10);
        Marbles marbles2 = new Marbles("James", 20);
        Marbles marbles3 = new Marbles("Kurt", 10);

        System.out.println(marbles1.compareTo(marbles2));
        System.out.println(marbles1.compareTo(marbles3));
    }
}
PreviousPolymorphismNextData Structures

Last updated 5 years ago

Was this helpful?