In this video we're gonna start talking about interfaces.
So an interface, in Java terms, specifies methods that a particular class that
implements the interface must implement.
Now the interface itself that you're creating is abstract, and
that means if there's no actual code for
any of the methods, you only supply the actual method names and the parameters.
The actual code still goes in the actual class that you're creating.
But the idea is to use an interface to provide a common behavior that
can be used by several classes by having them all implement the same interface.
So it's really a way to standardize the way a particular set of classes is used.
So looking at an example, if we have a look at a simple class that I've created,
called GearBox,
you can see that there's a private boolean field there called clutchIsIn.
And there's a simple public method there called operateClutch, which is sent
a parameter called in or out and if the string value is set to the value in then
clutch is in boolean field is actually set to true otherwise it's set to false.
So that's just one way of implementing a particular method to decide whether
the clutch is in, in a GearBox class, but
that's probably not the best way of doing it.
It's a little bit untidy.
So if we wanted to actually do something different,
we might decide that we wanted to actually change this here to boolean.
Like so, boolean in or out and
then we can do something like this clutches in equals in or out.
And that's certainly valid code and it does work, but
the problem would be then that the actual signature,
in other words this component of the public method, has been changed.
And if you've got some other code that is reliant on the fact that this
first parameter was a string, that's what it originally was, and
we've tramped it to a boolean code.
It means we've broken that other code in the sense that,
that then has to be changed.
That code has to be changed as well.
And if you're using a very popular framework or
your code is used in a lot of other places,
this could be a serious problem and it's not something that becomes immediately
aware if other programs are actually using your GearBox classes, class in example.
The problem actually is that although we've created this GearBox class,
we haven't really made a sort of commitment that won't be changed.
In other words we haven't said that once we create this particular public method,
we aren't going to change it.
So what the interface is is just that.
It's actually a commitment, a contract if you will, that the method's signatures and
that those variables in the interface constants if you defined any
will not change.
So consequently, you can create an interface and
use it and you'll know that provided you stick to what's in that interface,
the code isn't going to change and you're not gonna be breaking code anywhere.
So let's actually go ahead and
create an interface and talk more about how it actually works.
But we're now going to create an interface to define the behavior of a telephone.
So I'm gonna come up here to my package is normal click on new.
I'm actually going to select class, but then I'm going to come down here and
select interface.
So I'm going to actually type the interface name and
by convention you normally type I as the first letter, capital I for
the fact that it's interface.
So we're going to say I telephone, which means it's an interface named telephone.
And you notice there's a slight variation, GearBox the class has got public
class GearBox, but in interface uses the word interface instead of class.
Then what we actually do is we want to define just the methods,
in this case that we're going to be or that are gonna be used in this interface
that a class that implements this interface needs to actually implement.
So let's move ahead and actually do that.
So I'm gonna type public void power on,
that would be a function of a telephone to power it on.
Public Void Dial.
We're going to dial, we're going to set the parameter, phone number and
notice how I'm not actually creating classes.
I'm creating what's called the signature.
So this is the parameters, the name, the return type and any parameters, but
we're not actually defining the actual actual code itself.
That still goes into the class that inherits this interface.
So we're just defining the actual methods that form this interface.
So answer would be another one.
We'll try another one.
Public boolean call phone.
Number two in the status to see whether we're successful or not.
Boolean phone number would be the parameter.
Let's do another one just to determine whether the phone is ringing, IsRinging.
So that's a simple interface.
So again, all we've done here, we've said, for a class that is going to implement
them from this interface, these are the methods that it has to implement.
So you don't write code in the interface as such.
You still write that in the class, which you're about to see, but
we've just defined here the contract.
In other words, the contract and the actual parameters and return types that
are valid for each method, that overall will make this a valid class, effectively.
And notice also that public actually appears, and
it actually says it's redundant for interface methods.
And that actually makes sense if you think about it.
And that's because we're actually implementing the interface in a class, so
the interface exists to define the methods that must be implemented.
So in other words, private or public is really useless, in essence,
because you're gonna be doing that in the actual class.
So you could actually, if you wanted to, remove this altogether because we don't
actually need, whoops, we don't actually need that access modifier.
So that's quite valid to do that as well.
So now that we've actually done that,
how would we go about implementing this interface?
In other words, how do we go about to finding a class and
saying our class is going to be using this interface?
Well to do that we're going to create a new class first.
I'm going to create a new class called desk phone and
this will be a regular class, so just call it desk phone.
And the way you actually get it to use the interface is,
you use the implements keyword.
So you type implements, now type in R and
it should automatically find the interface.
You can see the I telephone interface is found for us.
So we've done that now.
As soon as we've actually typed that in, we're getting a red error.
And the error actually is class deskphone must either be declared abstract or
implement the abstract method powerOn, and
it's actually the those other methods as well.
So what it's actually saying is we need to start implementing some of these things
in order for this to become a valid class.
So there's a couple of ways forward,
you can actually do this automatically you can create the basic methods.
You can create stubs for these methods, which means you can create empty methods
and implement these if you want or you can type them manually.
So I'm going to actually type a few.
So I'm going to type MyNumber for starters which will be the phone number and
let's do another one private boolean IsRinging.
IsRinging.
So to go ahead and actually create them, you're going to actually go up and do,
notice here implement methods we haven't actually used that before but if we
actually click on that, it automatically went into the interface for our telephone,
and its found all these methods that says do you want to implement these.
I'm gonna click on, yes I'm just gonna leave the option to select forall of them,
click on okay, and you notice it's now overrode each one of those functions, and
suddenly the class is now quite happy because we've actually done them all.
Just to confirm, if we delete say the second one,
we'll get an error again because not all the methods have been implemented.
So again, what we're trying to do here is we're trying to say that
in order to be a valid class, we're saying that for
a telephone class that is in our application, these are the five methods
that must be implemented with the appropriate parameters.
And where appropriate, with the appropriate return types being void or
an actual Type.
And at that point we can think our way and actually create some code if we wanted to.
We could do something simple, for example with a desktop phone there's probably no
one button that's probably always on, so we could type something in there.
No action taken.
Desk phone does not have a power button.
For the dial now ringing.
So recording the functionality for this particular type of phone.
For answer, we want to check first to see whether it was ringing, so
we put if is ringing.
Answering the dis phone.
And we will set his ringing to false because we answered it.
Pretty simple.
And say a call phone if phone number is equal to my number.
IsRinging equals true.
IsRinging equals false.
And we'll return isRinging.
Just a very simple concocted example here just to show the concept of the interface.
And lastly, isRinging, we're gonna actually return the variable isRinging
to see whether that's true or false.
So although the interface relies on all these methods
even if you don't want to use it.
So for example, with the desktop phone, you might have decided
that we're not going to have a power button, you still need to implement it.
The implementation can be simple, in other words overriding that function
that's actually just putting an error message to say not supported or
not needed for this type of telephone if that makes sense.
So you can't sort of decide if an interface that you only want to implement
some of the methods.
In this case, if you're implementing this interface you have to
actually implement all the methods that's part and
parcel of the contract of ensuring that that's actually implemented.
So now let's go ahead and create an instance of the desktop class, so
we can do that by going into out main.
So we can go ahead on type Itelephone, and let's say Tim's phone.
And we could put Tim's phone = new.
Desk phone 123456, being the phone number.
Now the point I want to make here, now we actually got an arrow there.
I didn't create a constructor.
So I'll go ahead and actually create that constructor because we'll actually use
that so that should be handy to use.
And the constructor will actually just store the numbers.
So we'll do constructor.
We'll accept the actual number.
Can actually store that in our variable, in our field.
And back to main again, so that's okay.
And let's just call some of the functions.
So Tim's phone, .poweron.
Tim's phone dot call phone, let's just call the phone number the same number.
And Tim's phone.answer.
Now the point I want to make here is,
you can define the actual type to be the type of the interface, but
what you couldn't do we can't actually do a equals new our telephone.
That's not actually valid and Topic.
Not actually valid to do that because by doing that it knows automatically
what I did was it added an override order to create a class
within the implementation which we don't obviously want so I'm gonna undo that.
And if you think about that for a minute,
that actually makes sense why you can't do that.
[INAUDIBLE] And the reason that makes sense is we have to find an interface,
but the actual class implementation in this case desk
phone is the specific functionality for a certain type of telephone.
So the interface is there at an abstract level to tell you
what methods are valid and have to be actually overridden in an actual class.
So that's why you can't actually instantiate it using the I
telephone interface,
you have to actually use the actual class that has overridden that functionality.
So we could have actually also done this.
We could have changed this to desk phone.
That's certainly valid as well and that will work.
And I'll actually show you later on why you might actually need to do that in
certain circumstances.
But it is also valid to do it as I've done there,
to actually use the data type of type I telephoned this example in a definition,
provided you use the appropriate class that has
implemented the interface in a class, in a case rather, it was desk phone.
All right, so I'm going to end the video there.
In the next video we're going to actually continue on where we're at.
We're going to actually Implement a mobile phone class.
And then we are actually gonna go back to some of the code that we've done
previously in the course, specifically with the link list and
talk about an alternative way of doing that using the interface.
So I'll see you in the next video.