WEBVTT

00:00.050 --> 00:00.620
Hi everyone.

00:00.620 --> 00:01.310
Welcome back.

00:01.310 --> 00:07.430
In this lecture we will learn about the most interesting concept which is sealed classes or interfaces.

00:07.430 --> 00:10.700
I will start with some theory first and then we'll get to the coding part.

00:10.730 --> 00:15.170
This concept got released and it was generally available from Java 17.

00:15.170 --> 00:20.930
So if you are using Java 17 already, then you can leverage this concept and then start using it from

00:20.930 --> 00:21.530
now on.

00:21.530 --> 00:25.100
The basic idea is to allow inheritance by permission.

00:25.100 --> 00:28.130
So that's this concept is to restrict the inheritance.

00:28.130 --> 00:32.630
But you will still be allowed to apply inheritance by permission.

00:32.630 --> 00:35.060
Let me explain this using a simple example.

00:35.090 --> 00:38.660
As we all know, Java is very open by default.

00:38.660 --> 00:39.950
What do I mean by that?

00:39.950 --> 00:44.330
Any class can extend other class as long as it is accessible.

00:44.330 --> 00:51.410
In this example we have a vehicle class which is a superclass, and having a truck class and the car

00:51.410 --> 00:53.270
class extend the vehicle class.

00:53.270 --> 00:54.110
Make sense?

00:54.110 --> 00:57.230
But having a dog class extend the car class.

00:57.260 --> 00:59.600
That does not make sense, right?

00:59.600 --> 01:03.260
But technically, from a language perspective, you can still do it.

01:03.260 --> 01:09.140
There is no language constraint that's going to prevent us from doing it and listen until if a developer

01:09.140 --> 01:12.770
recognizes this and catches it before moving this code to production.

01:12.770 --> 01:17.540
But wouldn't it be nice if we have a language feature that can automatically prevent this for you?

01:17.570 --> 01:23.630
This is where sealed classes or interfaces comes into play to prevent this kind of behavior.

01:23.660 --> 01:28.700
Let's look at the same example with the addition of sealed keyword in our class hierarchy.

01:28.700 --> 01:31.520
So in this case we have the vehicle class.

01:31.520 --> 01:36.110
But if we take a look at it we have this sealed keyword added to the definition.

01:36.110 --> 01:41.780
So what this is going to do is that this is going to have another block where or another keyword it's

01:41.780 --> 01:45.590
going to permit only the car and truck.

01:45.620 --> 01:45.950
Right.

01:45.980 --> 01:51.980
So whatever that comes after the permits, those are the only classes which are allowed to extend the

01:51.980 --> 01:52.820
vehicle class.

01:52.820 --> 01:58.250
That's the actual definition of public sealed class vehicle permits car and truck.

01:58.250 --> 02:01.910
And here we have the final keyword added to the subclasses.

02:01.910 --> 02:06.680
And it is to make sure no more inheritance is further allowed on these classes.

02:06.680 --> 02:10.940
But there are other options available which I'll cover in the next lecture.

02:10.970 --> 02:16.710
Let's say in this case, if the dark class tries to extend the vehicle class, the compiler will complain

02:16.710 --> 02:21.900
about dark class being not part of the hierarchy and it is not allowed.

02:21.930 --> 02:23.370
I hope the explanation is clear.

02:23.370 --> 02:29.310
So the fundamental idea is to restrict inheritance from a language perspective, meaning applying the

02:29.310 --> 02:32.040
restriction while defining these classes.

02:32.040 --> 02:36.300
So with this information, let's go ahead and explore the sealed classes in action.

02:36.300 --> 02:37.740
Now let's go back to IntelliJ.

02:37.860 --> 02:38.520
There we go.

02:38.520 --> 02:43.650
So in the IntelliJ we are going to be working on the sealed package that you see over here.

02:43.650 --> 02:46.050
And then let's go ahead and open the vehicle class.

02:46.050 --> 02:51.150
So if you take a look at the vehicle class what we have is that this vehicle class act as a superclass

02:51.150 --> 02:55.710
for the car class, the dark class, and then the truck class.

02:55.710 --> 02:58.830
So actually we are going to be breaking this inheritance here.

02:58.830 --> 02:59.580
You can see right.

02:59.580 --> 03:01.620
The dog is extending the vehicle class.

03:01.620 --> 03:06.300
I just created this class on purpose so that we can actually understand the impact of this one.

03:06.300 --> 03:10.530
I'm explaining this concept using a very simple and naive implementations.

03:10.530 --> 03:15.150
In the future part of the course, there is a dedicated section where we will be building a real time

03:15.150 --> 03:19.340
application using all the concepts that we will be learning as part of this course.

03:19.370 --> 03:19.730
Okay.

03:19.760 --> 03:22.370
So now what I'm going to do, I'm going to go to the vehicle class.

03:22.370 --> 03:25.220
And in here I'm going to add the shield modifier.

03:25.220 --> 03:28.520
So when I add the shield modifier what this is going to do.

03:28.520 --> 03:30.920
So this is going to be giving me a compilation issue.

03:30.920 --> 03:35.330
So the compilation issue says add the missing subclasses to the permit classes.

03:35.330 --> 03:36.740
So I'm going to click on that one.

03:36.740 --> 03:38.450
So in this case what we need to do.

03:38.480 --> 03:40.460
We don't want the dark class to be part of it.

03:40.460 --> 03:40.700
Right.

03:40.730 --> 03:43.910
So we just need the car class and the truck class.

03:43.910 --> 03:49.100
And then it complains about car class being not final and car class being a regular class.

03:49.100 --> 03:51.740
So this is something which we'll look into it on.

03:51.740 --> 03:53.510
What are the different options for now?

03:53.510 --> 03:56.210
What we are going to do is we are going to add the final modifier.

03:56.210 --> 04:00.740
So what this final modifier is going to do, this is going to make this class final.

04:00.740 --> 04:04.010
And then stop allowing any kind of inheritance from here.

04:04.010 --> 04:07.970
So let's go back to the vehicle class and we'll do the same thing for the truck class.

04:08.600 --> 04:12.500
There are different options available which we will talk about in the next lecture as I mentioned.

04:12.710 --> 04:13.100
There we go.

04:13.100 --> 04:15.500
So the compilation issue is gone right.

04:15.530 --> 04:19.070
So now if you go to the dark class this is not going to be allowed.

04:19.070 --> 04:23.300
It says like a dog is not allowed in the sealed hierarchy.

04:23.300 --> 04:25.980
So in this case the compiler catches it, right?

04:26.010 --> 04:27.870
So in this case you go ahead and remove it.

04:27.870 --> 04:30.570
That's the only way to have this code compile.

04:30.570 --> 04:35.850
So with this code in place what we are achieving is that we are achieving the inheritance from the language

04:35.850 --> 04:36.270
itself.

04:36.270 --> 04:42.330
So when you define the class you make sure what are the different classes that can be part of the hierarchy.

04:42.330 --> 04:46.920
And you can achieve this by using the CL modifier and then the pyramids.

04:46.950 --> 04:53.130
Now let's learn about some of the techniques to write test cases to make sure the car and truck is part

04:53.130 --> 04:54.960
of the vehicle hierarchy.

04:55.020 --> 04:57.810
So now what I'm going to do I'm going to create a test case.

04:57.840 --> 04:59.640
So it's going to be the vehicle test.

05:00.000 --> 05:04.110
And then it's going to be under the sealed package in the test folder.

05:04.140 --> 05:05.550
Then what you do click okay.

05:05.580 --> 05:06.270
There you go.

05:06.300 --> 05:07.710
The test case is ready.

05:07.740 --> 05:08.940
I'm going to move this one to the right.

05:08.940 --> 05:10.110
In this case what we will do.

05:10.110 --> 05:12.960
We'll create a test case named let's add the annotation over here.

05:12.960 --> 05:15.810
And then the test case is going to be vehicle test.

05:15.840 --> 05:17.280
It's going to be vehicle.

05:17.280 --> 05:20.370
And what we are going to do is that we are going to create a car.

05:20.580 --> 05:24.570
It's going to be where car is equal to new car, okay.

05:24.600 --> 05:26.880
And then we are going to do the same thing for the truck.

05:26.910 --> 05:28.680
To where.

05:28.710 --> 05:29.520
Truck.

05:30.450 --> 05:31.800
New truck.

05:33.030 --> 05:33.450
Okay.

05:33.480 --> 05:37.380
And then how do we make sure this car is an instance of a vehicle?

05:37.410 --> 05:37.830
Right.

05:37.830 --> 05:41.760
So there is a assert instance of function which we can leverage.

05:41.760 --> 05:43.350
So assert instance of.

05:43.350 --> 05:49.110
So we can provide like a vehicle because we want to make sure car is part of the vehicle hierarchy.

05:49.110 --> 05:49.440
Right.

05:49.440 --> 05:52.620
So we can do this and then pass the instance car.

05:53.070 --> 05:55.740
And we can do the same thing over here.

05:55.740 --> 05:57.240
And this is going to be truck.

05:57.240 --> 06:01.530
So this is how we make sure we have test cases that's going to assert this behavior.

06:01.560 --> 06:01.890
Right.

06:01.920 --> 06:03.750
So we have defined the hierarchy here.

06:03.750 --> 06:09.210
But we need to make sure with a sealed and permit is it really going to work as expected.

06:09.210 --> 06:11.640
So in this case run the test case.

06:11.640 --> 06:12.120
There you go.

06:12.150 --> 06:13.080
The test case passed.

06:13.080 --> 06:17.310
That means the car and truck is part of the vehicle hierarchy.

06:17.310 --> 06:19.320
So this is how you create sealed classes.

06:19.320 --> 06:24.090
And the fundamental concept is to allow inheritance by permission.

06:24.090 --> 06:27.930
So this is a handy feature I have used this in many of my projects.

06:27.930 --> 06:33.450
It's really helpful if you have this kind of hierarchy related code, you should start using the sealed

06:33.450 --> 06:35.190
concept in your project two.

06:35.220 --> 06:36.600
This marks the end of this lecture.

06:36.600 --> 06:37.800
Thank you for watching.
