Practice with Interfaces : 41
Implementing Interfaces : 40
Using Interfaces : 39
Working with Exceptions : 38
Throwing Exceptions : 37
Catching Exceptions : 36
References and Polymorphism : 35
References : 34
Data Modeling 2 : 33
Equality and Object Copying : 32
Polymorphism : 31
Inheritance : 30
Data Modeling 1 : 29
Static : 28
Encapsulation : 27
Constructors : 26
Objects, Continued : 25
Introduction to Objects : 24
Compilation and Type Inference : 23
Practice with Collections : 22
Maps and Sets : 21
Lists and Type Parameters : 20
Imports and Libraries : 19
Multidimensional Arrays : 18
Practice with Strings : 17
null : 16
Algorithms and Strings : 15
Strings : 14
Functions and Algorithms : 13
Practice with Functions : 12
More About Functions : 11
Errors and Debugging : 10
Functions : 9
Practice with Loops and Algorithms : 8
Algorithms I : 7
Loops : 6
Arrays : 5
Compound Conditionals : 4
Conditional Expressions and Statements : 3
Operations on Variables : 2
Variables and Types : 1
Hello, world! : 0
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!
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.
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
!
Interactive Walkthrough
Click on an icon below to start!
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!
Interactive Walkthrough
Click on an icon below to start!
Practice: Last Odd Interface
Created By: learncs.online Staff
/ Version: 2020.10.0
Create a public class LastOdd
that implements the following interface:
Please Log In to Complete Homework Problems
This is a distinction that can be tricky for people.
So let’s go through an example together and discuss the differences.
Interactive Walkthrough
Click on an icon below to start!
Homework: Running Total Interface
Created By: learncs.online Staff
/ Version: 2021.10.0
Create a public class RunningTotal
that implements the following interface:
Please Log In to Complete Homework Problems
More Practice
Need more practice? Head over to the practice page.