Java Fundamentals

Interfaces

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Interfaces" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses interfaces as stateless constructs with abstract behaviors and demonstrates creating an interface and a class that implements it. By default, methods in interfaces are considered abstract.

Preview
Close

Transcript from the "Interfaces" Lesson

[00:00:00]
>> So in this section we'll talk about interfaces. An interface is similar to an abstract class, it's a template of an abstract concept. However, interfaces are meant to be purely abstract, and therefore they do not have state. This means interfaces they do not have constructors, the values of their fields cannot be changed, in other classes implement interfaces as opposed to extending them.

[00:00:37]
So we're going to create a product interface and then we're gonna create a book class that will implement that product, okay? So we'll make a new package, we'll call our package interfaces. Inside of here, we're gonna make a new Java class. Notice here, at the bottom, we seek class interface and then a few others, right?

[00:01:19]
So interface is what we wanna select this time. So you see that it is different than just a normal class, and we'll call this interface product. Now, if you've ever created an interface you forgot to do, change it from class to interface that's okay, it's simply just this word so you would just change that to interface.

[00:01:46]
Now, we think about a product, we could think of the fields. That's what we've been doing in other classes, we think about what attributes does that have? For example, maybe a product has a name. You don't want to even add fields really in interfaces. Notice I'm already getting an error here about it can't be private.

[00:02:15]
I mean I could take that off, but then I also get another error and basically it's saying this hasn't been initialized, that's because they are final, fields are final. So you would only add a field in an interface if you already know the value of that field and it should not ever be changed to anything else.

[00:02:40]
So in most cases, you wouldn't even need to have fields in an interface, all right? Let's go ahead and make a couple of methods, we'll say string get name after the parentheses close that with a semicolon and then a void setName. So I don't have the field for name, but that's okay because I want name to be modifiable by other classes.

[00:03:14]
And if I would have defined it in this interface I would have said the name of the product can only be this, and that's not what I want, right? So I've added these two methods by default, these two methods are abstract. I don't have to write the word abstract because that's just what they are in interfaces.

[00:03:36]
So you would be writing that in front of every method basically, and so Java saves you that trouble. So this is our interface, let's go ahead and make another regular class that will implement this interface. So we'll call that one book Now, in order to make the relationship between book and product, again, you do not extend interfaces.

[00:04:08]
You're not inheriting from it like that, we are doing this implementing an interface. That interface only has abstract methods, my job is to implement those abstract methods. So the keyword you use here is implements, not extends. And then you specify the interface that you want to implement. Notice here we get an error right away, it's gonna tell us, hey, you got those abstract classes, right?

[00:04:41]
This is the same thing, so an interface is abstract by default has abstract methods in it. So in whatever I need to do, in this case, I have two options. What are they?
>> Make it abstract or implement.
>> Yes, so I can make the book class abstracted itself, and then I wouldn't be on the hook, or I need to implement those abstract methods.

[00:05:10]
So we are going to actually implement those abstract methods. And I'm gonna just click this implement methods here and say, yep, I want both of those. Notice the implementation is not what we want, but we'll address that in a moment. Let's go ahead and make some fields for the book.

[00:05:31]
So we want to make that name field that is implied in the interface. We want things that are specific to a book author, maybe the pages of the book and then an ISBN. And we can make some getter and setter methods for all of those. The get and set for the name, notice it didn't create that, cuz that already exists, and it basically existed in that interface.

[00:06:21]
So we've overwritten that, so we can just change this implementation to return name and here we'll set the name. Okay, good? All right, let's make another class. And this is to test it out, we'll call this one library. Make a main method. Now, just like abstract classes, you can use the interface as a type.

[00:07:09]
However, you cannot instantiate interfaces. So I can say product book =, if I tried to say new product, I would get an error, it's abstract, it cannot be instantiated. So I would need to use one of the implementing classes. Okay, and then I can call that set name.

[00:07:38]
So that was again, the method that we inherited or essentially implemented from the interface. So I now put your favorite book, this is not my favorite book, but it's a common one. All right, okay.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now