Object oriented programming with Java, interfaces. In this chapter, we're going to compare interfaces to abstract classes. We're going to learn to define and implement an interface, discuss the design implications of interfaces, discuss inheritance with interfaces, discuss polymorphism, and I've been looking forward to this module for quite some time in the development of this course, because this is a really important discussion that we're going to be having over the next several lessons. Interfaces and abstract classes. On some levels, an interface is very similar to an abstract class, but there are crucial differences. An interface, in terms of attributes can only have final attributes, things that are static and belong to the interface because there can never be an instance. An interface can only have abstract methods, although Java 8 and later have added some things. We have a reading on that that you'll come across later in these lessons. Java 8 in particular adds default methods and static methods, in addition to the regular abstract methods that need to be implemented by classes on behalf of their instances. The syntax for creating an interface is slightly different than for an abstract class. For one thing, we use the keyword interface instead of class. Now here's where the big stuff shows up. Classes can only inherit from one parent class, but you can implement as many interfaces as necessary. Interfaces inherit from other interfaces, cannot inherit from classes. Interfaces can inherit, just like classes could, from more than one interface. So this is what helps make interfaces the key to polymorphism in Java. Here's an example using an interface. We used and discussed this example earlier, but we're revisiting it now that we're going to focus on interfaces. We've got car, which is a class, cargo is a class, sports car, SUV, and station wagon. The contrived example here is that we want to have sports car and SUV be polymorphic with respect to some convertible functionality. But not all cars are convertibles. We're going to say sports cars and SUVs are convertibles. That is a relationship. Because sports car is a car, sports car is a convertible according to the way we're writing this diagram. SUV is a convertible. Again, contrived example, but this is what we're trying to show you. How would you possibly work this in to make the same expression without an interface? There's no way to do it in class. You'd have to make all cars convertible. What about other things? Interfaces allow us to mix and match across and into, otherwise disjoint hierarchies. How do you define an interface? You use the interface keyword. You declare methods by listing access. Generally we can leave that off. It's going to be public, return type, the name of the method, its list and a semicolon instead of a code block. Now, in Java 8 and later, we also have the ability to have default methods. We will revisit that later in this module. To create a default method, we use the keyword default. If a class implements an interface with a default method, it can opt to implement that method. Otherwise, the compiler will do it for us and will make it available. But because it's an interface, a default method implementation can only be written in terms of other methods or static members, not instance data, because there is no instance data, this is an interface. To declare a static method, use the keyword static. Static methods are similarly limited to other static members, not even regular methods because regular methods will execute in the presence of an instance. A static method is only executing in the context of a class or an interface. Again, that's our limitation. Same limitation you would have with any other static method. So that's just our start of looking at interface. Next, we'll look at an example.