WEBVTT

00:00.140 --> 00:00.680
Hi everyone.

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

00:01.310 --> 00:06.920
In this lecture we will code and learn about the different options that are available for subclasses

00:06.920 --> 00:08.780
that extend a sealed class.

00:08.780 --> 00:14.930
So far we have seen the car class extending the vehicle class and the truck class extending the vehicle

00:14.930 --> 00:22.940
class and the final modifier in the car class ensures that no other class can extend the car class right,

00:22.940 --> 00:24.920
so there are no surprises over there.

00:24.920 --> 00:26.480
This is plain and simple.

00:26.480 --> 00:31.730
The next option is we can also add a sealed modifier to the car class.

00:31.730 --> 00:36.950
So this ensures that inheritance is allowed from the car class.

00:36.950 --> 00:43.220
This means other classes can still extend the car class, but controlled for classes that is defined

00:43.220 --> 00:45.230
after the permits keyword.

00:45.230 --> 00:47.330
So in this case we have a flying car.

00:47.360 --> 00:51.290
This means the flying car class can extend the car class.

00:51.290 --> 00:54.140
No other class is allowed to extend the car class.

00:54.140 --> 00:58.190
So what this means is that the subclasses can also be sealed.

00:58.190 --> 01:03.230
The next option is that you can also define the subclass to be Non-sealed.

01:03.230 --> 01:07.010
So in this case any class can extend the subclass car.

01:07.010 --> 01:12.560
So this disable the controlled inheritance behavior that comes with the sealed class.

01:12.560 --> 01:18.680
So this is to break the control inheritance and then opening up the car class to be extended by any

01:18.680 --> 01:19.520
other class.

01:19.520 --> 01:22.100
So basically these are the options that are available.

01:22.100 --> 01:23.840
Let's look at these in action.

01:23.840 --> 01:25.460
So I'm back in IntelliJ here.

01:25.460 --> 01:28.160
So in here we are in the vehicle class right now.

01:28.160 --> 01:32.720
So the very first thing that I'm going to do is I'm going to go to the car class and then mark this

01:32.720 --> 01:34.040
class as okay.

01:34.040 --> 01:37.700
So in this case as soon as I type in the sealed keyword.

01:37.700 --> 01:43.310
So what you have to do is you need to make sure you have permits present in this car class.

01:43.340 --> 01:49.340
So in this case if you go and mouse over here it tells us like sealed classes must have subclasses.

01:49.340 --> 01:51.950
So in this case we can have permits.

01:51.950 --> 01:54.320
I'm going to create a class named flying car.

01:54.440 --> 01:54.800
Okay.

01:54.830 --> 01:57.290
So flying car is one of the type of the car.

01:57.290 --> 02:00.440
So currently electric cars are the talk of the town.

02:00.440 --> 02:06.110
So maybe many generations from now you might see flying cars become really popular.

02:06.140 --> 02:08.210
So in this case let's go ahead and create this class.

02:08.210 --> 02:10.460
So we'll create the class named Flying Car.

02:10.460 --> 02:12.980
And it's going to be part of the seal package.

02:12.980 --> 02:18.230
And in this case this needs to be defined with non sealed final or sealed.

02:18.230 --> 02:21.620
So I'm going to mark this class with a final class modifier.

02:21.620 --> 02:23.990
So this resolves a compilation issue.

02:24.020 --> 02:25.670
Now if you go to the car class.

02:25.670 --> 02:30.800
So the car class is now a sealed class with one another class in the hierarchy.

02:30.800 --> 02:32.870
So this is one of the options that we saw.

02:32.900 --> 02:36.080
And this is how you actually implement the behavior.

02:36.080 --> 02:39.410
So now what I'm going to do I'm going to go to the vehicle class.

02:39.410 --> 02:43.400
And let's see how we can implement the non sealed modifier in the truck class.

02:43.400 --> 02:45.950
So in the truck class currently we have it as final.

02:45.950 --> 02:49.340
So what I'm going to do I'm going to comment this out and put it over here.

02:49.340 --> 02:51.800
And we can add the non sealed modifier.

02:51.800 --> 02:57.360
So what this actually means is that any other class can extend the class.

02:57.360 --> 02:59.970
So in this case, if you go to the dog class over here.

03:00.000 --> 03:03.480
Now dog class may be able to extend the truck class.

03:03.480 --> 03:04.350
Let's try that.

03:04.380 --> 03:05.820
So extend truck.

03:05.820 --> 03:11.760
So this is allowed as soon as you mark the subclass as Non-sealed this behavior will be allowed.

03:11.760 --> 03:14.190
But actually this is not the right practice.

03:14.190 --> 03:15.270
So I'm going to remove it.

03:15.270 --> 03:20.970
But what I want to show you is that when you mark the subclass as Non-sealed, you are going to open

03:20.970 --> 03:22.380
it up for any other class.

03:22.380 --> 03:28.110
To extend this class, it's totally up to the business functionality, how you want to implement your

03:28.110 --> 03:31.320
subclasses, and how restrictive you want the subclasses to be.

03:31.320 --> 03:37.320
So these are the options that we have when it comes to dealing with the subclasses of the sealed classes.

03:37.320 --> 03:40.800
So we saw the Non-sealed class and we saw the final one already.

03:40.800 --> 03:44.100
And then we saw the sealed class of the subclass two.

03:44.100 --> 03:45.960
So this is a very simple example.

03:45.960 --> 03:51.390
In the future part of the course, we are going to implement a real time example using all the concepts

03:51.390 --> 03:52.950
that we have learned so far.

03:52.980 --> 03:54.210
This marks the end of this lecture.

03:54.210 --> 03:55.410
Thank you for watching.
