Hi everyone.
Welcome back.
In this lecture we will code and learn about the different options that are available for subclasses
that extend a sealed class.
So far we have seen the car class extending the vehicle class and the truck class extending the vehicle
class and the final modifier in the car class ensures that no other class can extend the car class right,
so there are no surprises over there.
This is plain and simple.
The next option is we can also add a sealed modifier to the car class.
So this ensures that inheritance is allowed from the car class.
This means other classes can still extend the car class, but controlled for classes that is defined
after the permits keyword.
So in this case we have a flying car.
This means the flying car class can extend the car class.
No other class is allowed to extend the car class.
So what this means is that the subclasses can also be sealed.
The next option is that you can also define the subclass to be Non-sealed.
So in this case any class can extend the subclass car.
So this disable the controlled inheritance behavior that comes with the sealed class.
So this is to break the control inheritance and then opening up the car class to be extended by any
other class.
So basically these are the options that are available.
Let's look at these in action.
So I'm back in IntelliJ here.
So in here we are in the vehicle class right now.
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
class as okay.
So in this case as soon as I type in the sealed keyword.
So what you have to do is you need to make sure you have permits present in this car class.
So in this case if you go and mouse over here it tells us like sealed classes must have subclasses.
So in this case we can have permits.
I'm going to create a class named flying car.
Okay.
So flying car is one of the type of the car.
So currently electric cars are the talk of the town.
So maybe many generations from now you might see flying cars become really popular.
So in this case let's go ahead and create this class.
So we'll create the class named Flying Car.
And it's going to be part of the seal package.
And in this case this needs to be defined with non sealed final or sealed.
So I'm going to mark this class with a final class modifier.
So this resolves a compilation issue.
Now if you go to the car class.
So the car class is now a sealed class with one another class in the hierarchy.
So this is one of the options that we saw.
And this is how you actually implement the behavior.
So now what I'm going to do I'm going to go to the vehicle class.
And let's see how we can implement the non sealed modifier in the truck class.
So in the truck class currently we have it as final.
So what I'm going to do I'm going to comment this out and put it over here.
And we can add the non sealed modifier.
So what this actually means is that any other class can extend the class.
So in this case, if you go to the dog class over here.
Now dog class may be able to extend the truck class.
Let's try that.
So extend truck.
So this is allowed as soon as you mark the subclass as Non-sealed this behavior will be allowed.
But actually this is not the right practice.
So I'm going to remove it.
But what I want to show you is that when you mark the subclass as Non-sealed, you are going to open
it up for any other class.
To extend this class, it's totally up to the business functionality, how you want to implement your
subclasses, and how restrictive you want the subclasses to be.
So these are the options that we have when it comes to dealing with the subclasses of the sealed classes.
So we saw the Non-sealed class and we saw the final one already.
And then we saw the sealed class of the subclass two.
So this is a very simple example.
In the future part of the course, we are going to implement a real time example using all the concepts
that we have learned so far.
This marks the end of this lecture.
Thank you for watching.