Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • 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

    Using Interfaces

    public interface Math {
    // Return the sum of the two numbers
    int add(int first, int second);
    // Return first - second
    int subtract(int first, int second);
    }
    public class Mathematical implements Math {
    public int add(int first, int second) {
    return first + second;
    }
    }
    // Can you fix the compilation error?

    This topic is one of the most important we’ll cover in this course! Maybe we’ve said that before? But we really mean it this time. Interfaces are everywhere in computer science, and Java provides a great way to start understanding what they are and how they work. So let’s do it!

    abstract
    abstract

    But first, a short but important digression into one of the Java keywords that we have not yet covered: abstract. (Break out your Java keyword bingo cards!) Like final, abstract can be applied to both classes and methods, with somewhat different results. Let’s explore together:

    // Demonstrate how to use abstract

    We’ll return to abstract in a minute. But for now let’s move on!

    Interfaces
    Interfaces

    This lesson focuses on interfaces. Like references, interfaces are ubiquitous in computer science. But let’s look at the Wikipedia definition together:

    In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information. The exchange can be between software, computer hardware, peripheral devices, humans, and combinations of these. Some computer hardware devices, such as a touchscreen, can both send and receive data through the interface, while others such as a mouse or microphone may only provide an interface to send data to a given system.

    Every Object Has An Interface
    Every Object Has An Interface

    Just to be clear—while interfaces have a specific meaning in Java that we’re exploring, every Java object has an interface. Let’s see how:

    public class Course {
    private String department;
    private String number;
    public Course(String setDepartment, String setNumber) {
    department = setDepartment;
    number = setNumber;
    }
    public String getDepartment() {
    return department;
    }
    public String setNumber() {
    return number;
    }
    }

    Limitations of Inheritance
    Limitations of Inheritance

    As we’ve pointed out in the past, Java objects can only inherit from one class. This can lead to some problems as we design our classes.

    Let’s consider a real example: natural ordering. Some classes has a natural ordering, meaning that there is a well-established order. For example:

    However, not every class has such an ordering. For example, it’s not clear that the Pet classes we’ve been creating have a natural ordering.

    So this is an example of a feature that not every Java object has, meaning that we shouldn’t add it to Object. But whether or not an object can implement this feature doesn’t seem like it should constrain what it extends. Where does that leave us?

    interface
    interface

    Java interfaces provide a way for classes to declare that they support certain functionality. Unlike inheritance, a single class can implement more than one interface, while still extending another class. Let’s see how this works!

    interface Simple {
    int simple(int first);
    }

    interface References
    interface References

    Reference variables work similarly with classes that implement interfaces as they do with polymorphism and inheritance. Let’s look at an example:

    interface Simple {
    int simple(int first);
    }

    Remember abstract?
    Remember abstract?

    We can achieve something similar to interfaces using abstract, but it’s not quite the same. Let’s examine the differences:

    interface Simple {
    int simple(int first);
    }

    Comparable
    Comparable

    As we begin with interfaces, we’ll focus on using them first, and then discuss more about how to provide existing interfaces. Let’s look at one example of a useful built-in Java interface: Comparable.

    OurComparable
    OurComparable

    Java’s Comparable is what is called a parameterized interface. We’ve discussed type parameters previously, but we don’t want to tangle with them on interfaces quite yet. So, instead, when we work with Comparable we’ll use our own version called OurComparable:

    interface OurComparable {
    int compareTo(Object other);
    }

    This works identically to the official Comparable interface except without the type parameter. Now, let’s write some code together that uses the Comparable interface to make an existing (simple) algorithm more generic.

    interface OurComparable {
    int compareTo(Object other);
    }
    int returnLarger(int first, int second) {
    if (first > second) {
    return first;
    } else {
    return second;
    }
    }

    Practice: OurComparable Max

    Created By: Geoffrey Challen
    / Version: 2021.3.0

    Create a public class named Max with a single class method named max. max should accept an array of objects that implement OurComparable and return the maximum. Assert that the array is not null or empty.

    As a reminder, compareTo returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the passed object. OurComparable works exactly the same as the official Comparable interface, except it is slightly simplified and does not accept a type parameter.

    Homework: OurComparable Is Sorted

    Created By: Geoffrey Challen
    / Version: 2021.10.0

    Create a public class Sorted that provides a single class method named isSorted. isSorted accepts a list of OurComparables and returns true if the list is sorted in ascending order and false otherwise. If the passed list is null or empty you should throw an IllegalArgumentException.

    As a reminder, compareTo returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the passed object. OurComparable works exactly the same as the official Comparable interface, except it is slightly simplified and does not accept a type parameter.

    More Practice

    Need more practice? Head over to the practice page.