Chapter:Interfaces - Introduction

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Interfaces are used a lot since it is a lightweight inheritance. Using interfaces you can, a bit simplified, get the benefits from inheritance (using extends) and not get the downsides. This chapter introduces interfaces. Interfaces also allow you to write abstraction of unrelated classes.

Purpose

Make the student familiar with the interface concept. Also introduce and learn how to use some of the interfaces in the Java API.

Goal

The student shall understand:

  • what an interface is
  • how to implement existing interfaces
  • how to write and implement own interfaces

This is an introductory book. For this reason, this chapter doesn't cover the theory behind choosing between an interface and abstract classes, or even choosing between interfaces and inheritance using "extends". That topic is left for another book. Interfaces are commonplace in Java's API and in Java projects, however, so knowing what an interface is and how it works is something that the student should be prepared for. This is what this chapter aims to do.

Instructions to the teacher

Common problems

Students may get confused about when to use interfaces and why. A typical question could be "How should I know when to use interfaces, and when to use normal inheritance?". This question is not trivial to answer. From a language technical perspective, the simple answer would be that a class can only extend one other class, but implement many interfaces. So if a class already extends some class, it is kind of stuck in an inheritance hierarchy. But such a class can take on another form (another type) using the "implements SomeInterface" syntax. But starting a design from scratch is a whole lot more complex and complicated.

Refer to the existing interfaces of the Java API such as Comparable and Comparator, List, Collection etc in order to give some real life examples of real uses of interfaces. Explain to the student that making design decisions and designing class relations is something that takes a lot of experience. This introductory book simply goes through the semantics and syntax of interfaces and gives a few examples of how interfaces could be used.

Interfaces: Introduction

Sometimes we have a need to express some common capabilities of objects from otherwise totally unrelated classes. Often the need is to be able to speak about the objects in a highly abstract way, focusing solely on their capabilities, what they can do, i.e. what methods they offer to us.

A good example is to be found in the standard Java API where the authors found it good to express one such capability - the capability of Objects to compare themselves with other objects of the same type. The authors created an interface to express this capability - Comparable. An interface is like a super-abstract class which focuses on methods. The Comparable interface (in java.lang) declares only one abstract method, compareTo. A class which wants to signal that its objects have this capability, can be declared using the keyword implements and the name of the interface and the class name again:.

public class Member implements Comparable<Member>{
  // instance methods, constructors
  @Override
  public int compareTo(Member other){
    // code which compares this object with the other object
    // and returns an int as the result
    // 0 means they are to be considered equal
    // a negative value means that this object is less than the other
    // a positive value means that this object is greater than the other
  }
}

Quite a few of the classes in the API (some 57 classes or so) are declared in this way. A telling example is the class java.lang.String. String is declared as implements Comparable<String> (and some other interfaces actually), so String objects can be asked to compare themselves with other String objects. The compareTo() implementation in String returns value comparing the contents of the strings lexicographically which is a lot like alphabetical comparisons. This allows us to sort collections of Strings according to the spelling.

The choice of the name for this interface also allows us to talk about Strings in a way which is close to how we speak in plain English. We say that String objects "are comparable".

This turns out to be very useful, because this means that the interface Comparable<SomeClass> works as a promise about objects which have this type. All objects of a class which implements the Comparable interface will have a functioning compareTo() method implementation! This fact is used by some static methods for sorting lists and arrays. In order to sort the elements of an array, we need to compare all the elements to each other in some way, which is where the compareTo method comes in play. The authors of such a sorting method can use the Comparable interface as a contract, saying I can sort arrays if the elements are Comparable (that is, have the compareTo() method). The clients to the sorting method will have to fulfill their part of the contract and implement the compareTo() method.

The thing which is so good about this, is that using the abstraction of "being comparable", the authors of the sorting method don't have to have any other information about the elements in the array to sort, other than that the elements have a compareTo() method! So a good use of interfaces is that it allows us to agree on some capabilities of a group of objects, without caring at all about all the other information about these objects.

Another good thing about this, is that it is very easy for us to fulfill the contract of "being comparable". We simply have to read the documentation of the Comparable interface, and see what methods do we have to implement. In this case, only one method, the compareTo() method. So we can very easily create totally new classes which implement Comparable and they will work just fine with the sorting method which was written long before we even came up with the idea for our classes.

Another useful thing about interfaces, is that we can implement more than one interface, while we can only extend one class at the time using inheritance. Interfaces don't have many of the problems of classes using inheritance, because the interfaces work on a very abstract layer. They only list what methods must exist in classes implementing the interfaces. There is no hassle with constructors and initialization of objects (which we've seen exist using inheritance), because interfaces are not used for creating new objects. They are only meant to be used as a reference type based on behavior (what methods must exist).

Chapters on the topic of Interfaces

You can check your progress on this chapter using this list:

Videos

Note: There is only one English video for this chapter, as well as only one Swedish video. More may be added later.

English videos for this chapter

Swedish videos for this chapter

Look ahead! - Topic Interfaces - all videos from all chapters

All videos in this topic's chapters.

All interface chapters English videos

All interface chapters Swedish videos

See above and on each chapter page for individual links to the videos per chapter.

Links

Further reading

Where to go next

Next page has exercise for this chapter: Interfaces - Introduction - Exercises

« PreviousBook TOCNext »