KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Implementing Interfaces

import java.util.Random;
import java.util.Arrays;
public class Counter implements Comparable {
private int value;
public Counter(int setValue) {
value = setValue;
}
public void up() {
value++;
}
public void down() {
value--;
}
@Override
public String toString() {

Welcome back! Next we continue our journey with interfaces. In the last lesson we examined how to use interfaces. Now we’ll look at how to provide them in our own classes, and the kind of amazing things that this can unleash. Let’s go!

Debugging Challenge
Debugging Challenge

But let’s start with some debugging practice!

Implementing Interfaces
Implementing Interfaces

Last time we focused on using interfaces. In this lesson we’ll discuss implementing them ourselves. Along the way we’ll also discuss more about exactly how interfaces are so powerful.

Interfaces as Contract
Interfaces as Contract

As we begin to focus on using interfaces, it makes sense to think about an interface as a contract. When you implement an interface in one of your classes, you agree not only to provide certain methods, but also that these methods will do certain things!

Let’s return to our favorite Java interface—Comparable—for an example of how to read an interface like a contract.

Now, let’s put what we’ve learned to use by designing a new class and making it Comparable!

// Implementing Comparable

Interfaces as Abstraction Barrier
Interfaces as Abstraction Barrier

Another important way to think about interfaces is as something called an abstraction barrier. An abstraction barrier separates two parts of a program or system in ways that allow them to develop independently. Again, let’s return to Comparable to discuss exactly how that works!

// The Wide World of Comparable

Practice: Last Odd Interface

Created By: Geoffrey Challen
/ Version: 2020.10.0

Create a public class LastOdd that implements the following interface:

Using v. Implementing
Using v. Implementing

This is a distinction that can be tricky for people. So let’s go through an example together and discuss the differences.

// Using v. Implementing

Practice: Running Total Interface

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create a public class RunningTotal that implements the following interface:

Homework: Movable Location

Created By: Geoffrey Challen
/ Version: 2022.10.0

Create a public class named Location. Location should define a single public constructor that accepts two fields: a latitude and longitude position, as double values, in that order. Your constructor should reject invalid latitude and longitude values by throwing an IllegalArgumentException. Valid longitude values are between -180.0 and 180.0, inclusive, while valid latitude values are between -90.0 and 90.0.

Provide getters (but not setters) for the latitude and longitude following our usual conventions.

Your class should also implement the IMovableLocation interface:

CS People: Rachel Kroll
CS People: Rachel Kroll

Rachel Kroll may not be a household name. But she’s both an incredibly talented system administrator and a prolific author, having authored over 1000 posts on “software, technology, sysadmin war stories, and more.” Her writing is invariably both technically sharp, and infused with the wry humor of someone who has had a great many opportunities to observe the foibles of both people and machines. Not all of her writing will necessary make sense to you at first—but it will be there waiting once you have more experience and can appreciate it better, and likely largely as relevant in a decade as it is now.

Rachel Kroll has a few talks up online, but they’re long. So instead, please just read and enjoy this short piece commenting on code complexity. “Code runs on people. Please keep it simple.”

More Practice

Need more practice? Head over to the practice page.