Java Feature Spotlight: Sealed Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Java Feature Spotlight: Sealed Classes

Description:

Sealed classes: Developers were excited when this new feature was released in JDK15! They have been in use in other programming languages. Now it’s Java’s turn! See: – PowerPoint PPT presentation

Number of Views:53
Slides: 10
Provided by: Username withheld or not provided

less

Transcript and Presenter's Notes

Title: Java Feature Spotlight: Sealed Classes


1
BECOME AN XPERTI
HIRE AN XPERTI
EXPERTS
CLIENTS
HOW IT WORKS
BLOG
Type and hit enter...
?
Java Feature Spotlight Sealed Classes NOVEMBER
23 , 2020
RECENT POSTS
Java Security Vulnerabilities Case
Commentary What is TornadoVM? Getting
Comfortable With FPGAs Java Feature Spotlight
Sealed Classes The New Java Roadmap Whats In
Store For The Future?
ARCHIVES
December 2020
Java technology is known for its regular and
frequent releases, with something new developers
can look forward to each time. In September 2020,
it introduced another one of these exciting new
Java features. The release of Java SE 15 included
sealed classes (JEP 360) as a preview feature.
It is a prominent Java feature. This is because
the introduction of sealed classes in Java holds
the solution to a problem Java has had from its
initial 1.0 version, released 25 years ago.
November 2020
October 2020
September 2020
See Also Looking Back On 25 Years Of Java Major
Milestones
August 2020
  • Table of Contents
  • Context
  • Sealed Classes
  • How To Define A Sealed Class

?
July 2020
June 2020
2
  • Features Offered By Sealed Classes
  • Extensibility And Control
  • Simplification
  • Protection
  • More Accessibility
  • Exhaustiveness
  • Significance Of Sealed Classes
  • Sum And Product Types
  • Conflict With Encapsulation?
  • Sealed Classes And Records
  • Records And Pattern Matching
  • Conclusion

May 2020
April 2020
March 2020
February 2020
CATEGORIES
Articles
Context
Blog
Press Release
As we all know, one of the fundamentals of
object-oriented programming is inheritance. Java
programmers have been reusing fields and methods
of existing classes by inheriting them to new
classes. It saves time and they dont have to
write the code again. But things change if a
developer does not want to allow any random class
to extend his/her created class. This developer
can seal a class if he/she doesnt want any
clients of his/her library declaring any more
primitives. By sealing a class, Java Developers
can now specify which classes are allowed to
extend, restricting any other arbitrary class
from doing so.
Uncategorized
CONNECT FOLLOW
?
?
?
?
Sealed Classes
NEWSLETTER Subscribe to newsletter
now! Your email address.. SUBSCRIBE
Sealed classes in Java primarily provide
restrictions in extending subclasses. A sealed
class is abstract by itself. It cannot be
instantiated directly. But it can have abstract
members. Restriction is not a new concept in
Java. We are all aware of a Java feature called
final classes. It has already been offering
restricting extension but the addition of sealed
classes in Java can be considered a
generalization of finality. Restriction primarily
offers two advantages
Developers get better control over the code as he
or she can now control all the implementations,
and
The compiler can also better reason about
exhaustiveness just like it is done in switch
statements or cast conversion.
CHECK OUR TWEETS
3
How To Define A Sealed Class
Whoever said the path to career success is long
and stony never tried Xperti. Excellent career
opportunities for Am https/ t.co/fJdkbHaKHB ?
? ? 5 DAYS
To seal a class, we need to add the sealed
modifier to its declaration. After that, a
permits clause is added. This clause specifies
the classes that are allowed to be extended. It
must be added after any extends and implements
clauses.
Enlightened, ambitious, and ready for a great
challenge! Xperti wishes you a joyous Hanukkah
Festival 2020! https/ t.co/Cf9DmnHj6W ? ?
? 6 DAYS
Below is a very simple demonstration of a sealed
class in Java.
1. 2. 3. 4.
public sealed class Vehicle permits Car, Truck,
Motorcycle ... final class Car extends Vehicle
... final class Boat extends Vehicle ...
final class Plane extends Vehicle ...
Excellent opportunities for your technology
career, only with Xperti, Americas community of
top 1 tech talent. Si https/
t.co/EkofL2C0gE ? ? ? 7 DAYS
In the example above, Vehicle is the name of a
sealed class, which specifies three permitted
subclasses Car, Boat and Plane.
There are certain conditions for declaring a
sealed class that must be fulfilled by the
subclasses
_at_XPERTI1
1. The subclasses must be in the same package or
module as the base class. It is also possible to
define them in the same source file as the base
class. In such a situation, the permits clause
will not be required.
LATEST POSTS
2. The subclasses must be declared either final,
sealed or non-sealed.
Java Security Vulnerabilities Case
Commentary DECEMBER 15 , 2020
The permits list is defined to mention the
selected classes that can implement Vehicle. In
this case, these classes are Car, Boat and
Plane. A compilation error will be received if
any other class or interface attempts to extend
the Vehicle class.
Features Offered By Sealed Classes
What is TornadoVM? DECEMBER 10, 2020
  • Extensibility And Control

Sealed classes offer a new way to declare all
available subclasses of a class or interface.
Its a handy Java feature. Especially if a
developer wishes to make superclasses accessible
while restricting unintended extensibility. It
also allows classes and interfaces to have more
control over their permitted subtypes. This can
be useful for many applications including general
domain modelling and for building more secure
and stable platform libraries.
Getting Comfortable With FPGAs DECEMBER 2, 2020
Java Feature Spotlight
4
  • Simplification

Sealed Classes NOVEMBER 23 , 2020
Another noticeable advantage of introducing
sealed classes in Java is the simplification of
code. It greatly simplifies code by providing an
option to represent the constraints of the
domain. Now Java coders are not required to use a
default section in a switch or a catch-all
else block to avoid getting an unknown type.
Additionally, it also seems useful for
serialization of data to and from structured
data formats, like XML. Sealed classes also allow
developers to know all possible subtypes that
are supported in the given format. (And yes, the
subtypes are not hidden. This will be discussed
in detail later in the article.)
INSTAGRAM
  • Protection

Sealed classes can also be used as a layer of
additional protection against initialization of
unintended classes during polymorphic
deserialization. Polymorphic deserialization has
been one of the primary sources of attacks in
such frameworks. These frameworks can take
advantage of the information of the complete set
of subtypes, and in case of a potential attack,
they can stop before even trying to load the
class.
  • More Accessibility

The typical process of creating a new class or
interface includes deciding which scope modifier
must be used. It is usually very simple until the
developers come across a project where using a
default scope modifier is not recommended by the
official style guide. With sealed classes,
developers now get better accessibility, using
inheritance with a sealed scope modifier while
creating new classes. In other words, with the
entry of sealed classes in Java, if a developer
needs the super class to be widely accessible but
not arbitrary extensible, he/she has a
straightforward solution.
View on Instagram
CATEGORIES
Articles (1)
Blog (45)
Press Release (1)
Uncategorized (1)
Sealed classes also allow Java libraries authors
to decouple accessibility from extensibility. It
provides freedom and flexibility to developers.
But they must use it logically and not overuse
it. For instance, List cannot be sealed, as
users should have the ability to create new kinds
of Lists. It makes sense for developers to not
seal it.
  • Exhaustiveness

Sealed classes also carry out an exhaustive list
of possible subtypes, which can be used by both
programmers and compilers. For example, in the
defined class above, a compiler can extensively
reason about the vehicles class (not possible
without this list). This information can
5
Significance Of Sealed Classes
be used by some other tools as well. For
instance, the Javadoc tool lists the permitted
subtypes in the generated documentation page for
a sealed class.
Sealed classes rank highly among the other
features released in Java 15. Jonathan Harley, a
Software Development Team Leader provided an
excellent explanation on Quora about how
important sealed classes can be for Java
Developers The fact that interfaces, as well as
classes, can be sealed is important to
understand how developers can use them to improve
their code. Until now, if you wanted to expose
an abstraction to the rest of an application
while keeping the implementation private, your
only choices were to expose an interface (which
can always be extended) or an abstract class
with a package-private constructor which you hope
will indicate to users that they should not
instantiate it themselves. But there was no way
to restrict a user from adding their
constructors with different signatures or adding
their package with the same name as yours. He
further explained, Sealed types allow you to
expose a type (interface or class) to other code
while still keeping full control of subtypes, and
they also allow you to keep abstract classes
completely private
Sum And Product Types
The example mentioned earlier in the article
makes a statement about how a Vehicle can only
either be a Car Boat or a Plane It means
that the set of all Vehicles is equal to the set
of all Cars, all Boats and all Planes combined.
This is why sealed classes are also known as sum
types. Because their value set is the sum of
the value sets of a fixed list of other types.
Sum types, and sealed classes are new for Java
but not in the larger scale of things.Scala and
many other high-level programming languages have
been using sealed classes too, as well as sum
types for quite some time.
6
Conflict With Encapsulation?
Object-oriented modelling has always encouraged
developers to keep the implementation of an
abstract type hidden. But then why is this new
Java feature contradicting this rule? When
developers are modelling a well-understood and
stable domain, encapsulation can be neglected
because users will not be benefitting from the
application of encapsulation in this case. In
the worst possible scenario, it may even make it
difficult for clients to work with a very simple
domain. This does not mean that encapsulation is
a mistake. It just means that at a higher and
complex level of programming, developers are
aware of the consequences. So they can make the
call to go a bit out of line to get some work
done.
Sealed Classes And Records
Sealed classes work well with records (JEP 384).
Record is a relatively new Java feature that is
a form of product type. Records are a new kind of
type declaration in Java similar to Enum. It is
a restricted form of class. Records are
implicitly final, so a sealed hierarchy with
records is slightly more concise. To explain
this further, we can extend the previously
mentioned example using records to declare the
subtypes
sealed interface Vehicle permits Car, Boat, Plane
record Car (float speed, string mode)
implements Vehicle ... record Boat (float
speed, string mode) implements Vehicle ...
record Plane (float speed, string mode)
implements Vehicle ...
1. 2. 3. 4.
This example shows how sum and record (product
types) work together we can say that a car, a
plane or a boat is defined by its speed and its
mode. In another application, it can also be used
for selecting which other types can be the
subclasses of the sealed class. For example,
simple arithmetic expressions with records and
sealed types would be like the code mentioned
below
1. 2. 3. 4.
sealed interface Arithmetic ... record
MakeConstant (int i) implements Arithmetic
... record Addition (Arithmetic a, Arithmetic
b) implements Arithmetic ... record
Multiplication (Arithmetic a, Arithmetic b)
implements Arithmetic
7
... 5. record Negative (Arithmetic e)
implements Arithmetic ... Here we have two
concrete types addition and multiplication which
hold two subexpressions, and two concrete types,
MakeConstant and Negative which holds one
subexpression. It also declares a supertype for
arithmetic and captures the constraint that these
are the only subtypes of arithmetic. The
combination of sealed classes and records is also
known as algebraic data types. Records allow
us to express product types, and sealed classes
allow us to express sum types.
Records And Pattern Matching
Both records and sealed types have an association
with pattern matching. Records admit easy
decomposition into their components, and sealed
types provide the compiler with exhaustiveness
information so that a switch that covers all the
subtypes need not provide a default clause. A
limited form of pattern matching has been
previously introduced in Java SE 14 which will
hopefully be extended in the future. This initial
version of Java feature allows Java developers
to usetype patternsin instanceof. For example,
lets take a look at the code snippet below
if (vehicle instanceof Car c) // compiler has
itself cast vehicle to the car and bound it to c
System.out.printf("The speed of this car is
dn", c.speed())
1. 2. 3.
Conclusion
Although many other tools were released with
sealed classes, it remains the most prominent
Java feature of the release. We are still not
certain about the final representation of sealed
classes in Java. (It has been released as a
preview in Java 15). But so far sealed classes
are offering a wide range of uses and
advantages. They prove to be useful as a domain
modelling technique, when developers need to
capture an exhaustive set of alternatives in the
domain model. Sealed types become a natural
complement to records, as together they form
common patterns. Both of them would also be a
natural fit for pattern matching. It is obvious
that sealed classes serve as a quite useful
improvement in Java. And, with the overwhelming
8
response from the Java community, we can expect
that a better, more refined version of sealed
classes in Java will soon be released.
JAVA FEATURE
SEALED CLASSES
SEALED CLASSES IN JAVA
? 0
?
?
? ?
AUT HOR Shaharyar Lalani Shaharyar Lalani is a
developer with a strong interest in business
analysis, project management, and UX design. He
writes and teaches extensively on themes current
in the world of web and app development,
especially in Java technology. ?
RELATED POSTS
Java Security Vulnerabilities Case
Commentary DECEMBER 15 , 2020
Getting Comfortable With FPGAs DECEMBER 2 , 2020
What is TornadoVM? DECEMBER 10, 2020
WRITE A COMMENT
Name
Email
Website
9
Enter your comment here..
Save my name, email, andwebsite
inthisbrowserforthe nexttime I comment.
POST COMMENT
SKILLSETS IN DEMAND
QUICK LINKS
CONNECT
Designers Developers Project Managers Quality
Assurance Business Analysts
Home Experts Clients FAQ's Privacy Policy
Contact Us
? ? ? ?
Copyright 2020. All rights reserved by Xperti
PDFmyURL.com - convert URLs, web pages or even
full websites to PDF online. Easy API for
developers!
Write a Comment
User Comments (0)
About PowerShow.com