Sealed Classes-Approaching Restrictive Inheritance

Aman Vijay
The Jabberjays
Published in
5 min readNov 18, 2020

--

Image Source:Google Images

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. One of the core features of object oriented programming is inheritance in which one object acquires all the behaviour and properties of a parent object. It is used to increase reusability of code. The feature is great but there is a drawback that any class can inherit the properties of our base class by extending it. However we can make a class final so that no other class will be able to inherit it, but then it won’t help in creating reusable code.

Let’s say we are creating a base class named as Animal and we want other developers to use the functionalities of our base class. So other developers just have to extend the base class and they will inherit all the functionality defined in the base class. Now a developer created a class named as Dog, since dog is also an animal so the developer extends dog class with animal class and inherits all the functionality of animal class. Now there is another developer who is creating a class named as Person and he wants to inherit a method of our base class animal( let’s say a method named as walk() since both person and animal walk). This is fairly possible while writing code, but if talk in a real world scenario then a person is not an animal, so a class named as Person should not inherit the property of a class named as Animal. In brief we can say that we want to restrict access, that is we only want a certain sub classes to extend our base class. We can not make our base class final because then no other class will be able to inherit it. To rescue us from this problem, java in jdk-15 has provided us with a feature known as Sealed class.

As the name suggests with sealed class we can seal a class, as in we can say that which sub-classes are permitted to extend our base class. By allowing restrictive access to our base class we are implementing a hierarchical, cleaner and restricted design.

Let’s try to understand the concept of Sealed classes with the above example where in we have animal as our base class and two sub classes Dog and Person inheriting it.

In the given image, we can see that both Dog and Person class are able to inherit the properties of Animal class and this code will work absolutely fine as well. But if think in a real world scenario, the Person class and Animal class are no where related and we want to restrict access to our Animal class.

The way to make a class as sealed to class is by using a sealed keyword. The moment we write sealed there is an error in the classes which are inheriting the sealed class. The reason for this error is because we have not allowed the sub classes to inherit the property of sealed class. The way to resolve this error is by allowing access to sub class. The way to do this is using the permits keyword.

Now we have permitted the Dog class to inherit the properties of it’s base class Animal but there is another error which is “sealed, non-sealed or final modifiers expected”. Before diving into solving this error let’s understand what are these three modifiers.

Sealed

When we want to allow only certain sub class to inherit the properties of our base class then we will use sealed, where in we will write permits after class name followed by comma separated values of classes to permit.

Non- Sealed

When we want any number of classes to extend our base class then non-sealed modifier will be used. Here the class is prefixed by keyword non-sealed.

Final

when we don’t want any subclass to inherit the properties of our base class then we will use final modifier. Here the class is prefixed by keyword final.

Coming back to the error we got, the permitted sub class must have exactly one of the above modifiers to continue the sealing initiative by it’s base class. So if want other sub classes to inherit the Dog class then the modifier will be sealing, if we don’t want any restrictive access on the Dog class then the modifier will be non-sealed and lastly if we don’t want any other class to inherit our dog class then the modifier will be final. For the purpose of this article let’s not further extend the properties of Dog class, so we will make it final.

There is an error in our Person class which is inheriting the properties of our base class Animal, the error is because we have not allowed Person class to inherit the properties of our base class Animal. So the person class won’t have the access of the methods of base class Animal.

There are few pointers that we need to consider:

  • A sealed class and its permitted sub classes should be in the same package.
  • A permitted subclass should directly extend its super class.

In summary, it should be possible for a superclass to be widely accessible (since it represents an important abstraction for users) but not widely extensible (since its subclasses should be restricted to those known to the author).

--

--

Aman Vijay
The Jabberjays

An astrophile, trying to learn about universe.