Intro to Java programming, Unit 17: Interfaces

Hey there! Steve Perry here. Let’s talk about interfaces. In this video I’ll show you: * What an interface is
* How to define and implement an interface * And I’ll demonstrate some assignment rules
regarding interfaces versus classes Let’s get started. In the unit on inheritance, we looked a little
bit at the concept of an abstract method, which defines: * A name
* Parameter list * And return type Collectively, we call this the method’s contract. And we use that contract to design our software
so that when we call the method, we know what it’s supposed to do. However, we don’t know — or really care — how
it fulfills the contract. By now you’re familiar with java.util.List,
and we’ve seen it many times in the Learning Path. Well, did you know that List is an interface? Simply a well-defined and well-documented
set of behaviors. Let’s take a quick look at the javadoc for
List. As you can see, the behavior is extensive
and very well defined and documented. There are several implementations of List: * ArrayList
* LinkedList * Stack
* And Vector to name just a few. The point is that every implementation of
List is consistent because of the interface contract. Here are two examples of when we might use
an interface in our software: * When behavior is well-defined
* And there are numerous possible implementations It might help to think of it this way: A class
that has at least one abstract method must be declared abstract, so it’s an abstract
class. Think of an interface as an abstract class
with nothing but public abstract methods on it. However, the analogy breaks down in one important
way. A class may implement as many interfaces as
it wishes, but it may only have one direct superclass. Okay, now that we’ve talked a little about
what an interface is and isn’t, let’s talk about how to create one. For this video, I’ll use the interface from
the Learning Path, StockOptionEligible, which in our somewhat contrived Human Resources
application is implemented by all classes at the Manager level and higher. Let’s open Eclipse. With Eclipse open, let’s open our Tutorial
project — the one we created in Unit 5, and then augmented with the Employee class in
Unit 13. The first thing we need to do is define the
StockOptionEligible interface. To create the interface, we right-click on
the package in which we want to create the interface, and choose New>Interface and
the New Java Interface dialog opens. Let’s type in the name of our new interface,
StockOptionEligible, and click Finish. Now we will write two classes: * Manager
* And Executive Again, we right-click the package and choose
New>Class. The name of the class is Manager. The superclass is Employee. And we’re going to do something different
here. Notice in the interface’s text view, we click
the Add… button and type in the name of our interface and click OK. Now click Finish. Notice that Eclipse generates an empty method
body for us, so the code compiles right away. It doesn’t do anything interesting, of course. So let’s take care of that now. Next we’re going to create the Executive class. Its superclass is Manager. Since Manager implements the StockOptionEligible
interface, we’re not required to implement it again, but let’s just do that for fun. So we talk in the Learning Path a little about
assignment rules, but it might be helpful to walk through a few examples, to highlight
the differences between assignment rules for interfaces versus classes. First, I’ll create a new test method in EmployeeTest
just to illustrate this, and I’ll just paste in the code to save time. Let’s remember back to when we learned about
assignment rules. When assigning an object reference type to
a type that is on the same hierarchy, you can never narrow the scope of the assignment,
meaning you cannot assign a superclass object reference to a variable of a type that is
a subclass. The reason for this is that it’s possible
to have a reference to a subclass — say, Guest — do a Person reference, and try to
assign that to a variable of type Employee. You can see right away this is not okay. You can, however, always make a widening assignment,
which is where you assign a subclass reference to a variable of a type that is a superclass
of the object. In the first three assignments, we see here
they are all narrowing assignments. The next assignment of Person to Employee
is widening, and therefore okay. The assignment of Manager to Employee and
Manager to Person, both widening assignments, are okay. However, the assignment of the Employee to
a Manager is not allowed, and so forth. I want to talk a little about interface reference
assignments. Notice that I assign an Executive to a variable
of type StockOptionEligible. That assignment is perfectly fine, since Executive
implements that interface. And the same goes for Manager. However, the assignment of Employee to StockOptionEligible
is not allowed. Why not? It’s simple: Employee does not implement the
StockOptionEligible interface. So hopefully you have a better idea of how
interfaces work, how to create and implement them, and some of the assignment rules and
how they vary between classes and interfaces. Well, that all the time we have for now. I’ll see you in the next video!

Tags: , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *