Lesson 18Diving into Interfaces
In this lesson, we will look at how interfaces are created and used.
Interfaces can help us integrate code from different teams and allow us to express loosely coupled, contract-based relationships between components. Here, we will cover declaration, implementation, and extension of interfaces. We will also look at the restrictions on interfaces and how interfaces work with polymorphism.
This is not an exhaustive treatment of interfaces. We're just going to cover the basics. As you start creating more advanced programs, you will learn how to apply interfaces in increasingly sophisticated situations, so this is just the beginning of our journey with interfaces.
WHAT IS AN INTERFACE?
An interface is a reference type, just like classes and enums. The big difference is that interfaces are never instantiated. That is to say, you cannot do this:
new SomeInterface()
What an interface does is define a set of methods that provide standard behaviors that any type that implements the interface must provide.
DECLARING AN INTERFACE
Declaring an interface is similar to declaring a class. Just as with a class, the interface must reside in a file with the same name as the interface and a .java
extension. For example, the ...
Get Job Ready Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.