An interface defines method that a class needs to implement.
If a class implements an interface, it must override all of the methods inside the interface.
In this lesson, one of your classes is going to implement an interface.
An interface is a contract, and the contract defines methods that a class needs to implement.
Use an interface to define behavior that's required from a class, because if a class implements an
interface, it must override all of the methods inside the interface.
You can define an interface the way you would define a class public interface, followed by the interface
name.
And inside the interface, you can define method signatures, a class that implements this interface
must override every method inside of its.
So looking back at the requirements, we see that pense are eligible for 50 percent discounts, but
shirts are not.
But what I want to do is force the pants class to implement this behavior.
So I'll define an interface, create a new file, discounted Java.
And inside this kind of job, Java make this an interface.
And we're going to force every class and implement this interface to override a public void method called
discount.
And now inside Penstock Java, we can say that the pants class implements the discount interface.
When a class implements an interface, it's basically signing a contract that it's going to override
all of the methods inside the interface.
And here we have to honor the contract to override discount.
And we're going to offer a 50 percent discount by saying superdog set price.
Superdog get price divided by two.
And now the air is gone.
So back in Maine, we can call Penn student discount.
Put a breakpoint, launched the debugger.
And the price of the object went down by 50 percent.
Now, what's really interesting is that interfaces allow for polymorphism.
Because pants are discounted so we can declare the object as type discounted.
Keep this in mind for your next quiz.
An interface is a contract.
It defines methods that a class needs to implement.
When a class implements an interface, it signs the contract, therefore the class must override all
of the methods inside the interface.