Abstract classes and interfaces
These videos explain abstract classes and interfaces in Java.
[Note: when you click an icon below, a fancy box will open in front of this page, with a red arrow in the middle; click the red arrow to start the youtube video. To change the size of the window, e.g. make it bigger, drag THIS window, not the fancy box. Click the X in the upper right to close the fancy box.]
Don't be afraid to pause a video so you can read the screen, or "rewind" and watch something again!
If, after watching these videos, you still are not convinced of the need/use of interfaces, be patient. In the coming weeks, you will see them being used over and over again, and the interface wil finally appear as a basic component in OO programming. The total time of the video is well under 15 minutes!
Why make a class and a method abstract?
We explain what an abstract class is and why one makes a class and a method in it abstract.
Make a class abstract so one cannot create instances of it.
Make a method abstract so that a subclass must override (declare) it.
(3.5 minutes) Read about here: abstract.pdf
What is an interface?
We explain the concept of a Java interface. You can think of it as an abstract class all of whose methods are public and abstract. (See the end of this page for a short explanation of other possible components of an interface.) A class can extend only one other class, but it can implement many interfaces. (2.4 minutes) Read about here: interface.pdf
Three other components of an interface
If this is your first exposure to interfaces, there is no need to read this subsection. Besides abstract methods, interfaces may have three other kinds of component. We advise you not to use the first two until you are more experienced.
1. A constant: i.e. a public, static, final variable.
2. Starting in Java 8, an interface can have a default implementation of a method. Suppose an interface has this declaration:
default public String noise() { return bzzzzz"; }
If an implementing class does not override method noise, the default is used. Note: this method is not abstract.
3. Java automatically inserts some methods that are in superest class Object, making them abstract. This shouldn't affect you in any way; it's just good to be aware of this. We explain in detail below.
Consider an interface that has no direct superinterface. Suppose superest class Object contains a public instance method m with signature s, return type r, and throws clause t. If this interface does not override m, then a declaration of public abstract method m with signature s, return type r, and throws clause t is inserted into this interface.
Casting
We show you what an object that includes interfaces looks like and use that object to discuss casting to get different perspectives on the object. (3.75 minutes) Read about here: casting.pdf
Interface Comparable
We show you why interfaces are so important, using as the example interface java.lang.Comparable. Use of Comparable allows us to have only one procedure to sort arrays of any class as long as the class implements Comparable. (2.5 minutes) Read about here: Comparable.pdf Here's class Time and a testing class to demo this stuff: TimeDemo.zip
Default methods in interfaces and the diamond problem
In Spring 2014, version 8 of Java was introduced. Now, interfaces can have default methods. If a class implements an interface and doesn't implement a method that has a default implementation, the default implementation is used. This brings up issues of "multiple inheritance" ---inheritance of a method from several sources, which has to be resolved. This pdf file talks about the diamond problem and how it is solved in Java.