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 teh 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
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
Two other components of an interface
Besides abstract methods, interfaces can have two other kinds of component, but we advise you not to use them until you are more experienced.
1. A constant: i.e. a public, static, final variable. This component was used in early Java programs, but it's not considered good programming. Here's an example: public static final String GOLD= "Gold";
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 " "; }
If an implementing class does not override method noise, the default is used.
There were excellent reasons for introducing default methods into Java 8, but a discussion of this topic is outside the scope of this introduction. Moreover, conflicts can arise, and the way they are solved is also outside the scope of this introduction. An example of a conflict: Suppose a class inherits default method noise() from two different interfaces and those default methods return different values; which is used?