1
00:00:00,330 --> 00:00:03,420
An interface defines method that a class needs to implement.

2
00:00:06,070 --> 00:00:11,470
If a class implements an interface, it must override all of the methods inside the interface.

3
00:00:14,720 --> 00:00:17,990
In this lesson, one of your classes is going to implement an interface.

4
00:00:21,500 --> 00:00:26,930
An interface is a contract, and the contract defines methods that a class needs to implement.

5
00:00:29,410 --> 00:00:34,870
Use an interface to define behavior that's required from a class, because if a class implements an

6
00:00:34,870 --> 00:00:38,740
interface, it must override all of the methods inside the interface.

7
00:00:42,220 --> 00:00:47,410
You can define an interface the way you would define a class public interface, followed by the interface

8
00:00:47,410 --> 00:00:47,710
name.

9
00:00:50,310 --> 00:00:55,860
And inside the interface, you can define method signatures, a class that implements this interface

10
00:00:55,860 --> 00:00:58,440
must override every method inside of its.

11
00:01:02,760 --> 00:01:07,500
So looking back at the requirements, we see that pense are eligible for 50 percent discounts, but

12
00:01:07,500 --> 00:01:08,390
shirts are not.

13
00:01:08,430 --> 00:01:12,060
But what I want to do is force the pants class to implement this behavior.

14
00:01:15,970 --> 00:01:19,690
So I'll define an interface, create a new file, discounted Java.

15
00:01:23,220 --> 00:01:26,280
And inside this kind of job, Java make this an interface.

16
00:01:30,040 --> 00:01:35,680
And we're going to force every class and implement this interface to override a public void method called

17
00:01:35,680 --> 00:01:36,370
discount.

18
00:01:40,070 --> 00:01:44,750
And now inside Penstock Java, we can say that the pants class implements the discount interface.

19
00:01:51,400 --> 00:01:55,930
When a class implements an interface, it's basically signing a contract that it's going to override

20
00:01:55,930 --> 00:01:58,150
all of the methods inside the interface.

21
00:01:58,500 --> 00:02:01,810
And here we have to honor the contract to override discount.

22
00:02:09,710 --> 00:02:13,430
And we're going to offer a 50 percent discount by saying superdog set price.

23
00:02:15,570 --> 00:02:17,610
Superdog get price divided by two.

24
00:02:21,090 --> 00:02:22,440
And now the air is gone.

25
00:02:27,060 --> 00:02:29,940
So back in Maine, we can call Penn student discount.

26
00:02:31,790 --> 00:02:33,950
Put a breakpoint, launched the debugger.

27
00:02:45,420 --> 00:02:48,330
And the price of the object went down by 50 percent.

28
00:02:49,620 --> 00:02:52,980
Now, what's really interesting is that interfaces allow for polymorphism.

29
00:02:54,720 --> 00:02:59,200
Because pants are discounted so we can declare the object as type discounted.

30
00:03:02,210 --> 00:03:04,030
Keep this in mind for your next quiz.

31
00:03:07,670 --> 00:03:09,410
An interface is a contract.

32
00:03:11,310 --> 00:03:14,100
It defines methods that a class needs to implement.

33
00:03:16,010 --> 00:03:22,220
When a class implements an interface, it signs the contract, therefore the class must override all

34
00:03:22,220 --> 00:03:23,960
of the methods inside the interface.

