<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhagyashri Birajdar</title>
    <description>The latest articles on DEV Community by Bhagyashri Birajdar (@birajdar).</description>
    <link>https://dev.to/birajdar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1157303%2Fdc3d096c-601f-4298-8364-ccaf377fa6e1.png</url>
      <title>DEV Community: Bhagyashri Birajdar</title>
      <link>https://dev.to/birajdar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/birajdar"/>
    <language>en</language>
    <item>
      <title>Platform Thread vs Virtual Thread in JAVA</title>
      <dc:creator>Bhagyashri Birajdar</dc:creator>
      <pubDate>Tue, 03 Feb 2026 18:07:24 +0000</pubDate>
      <link>https://dev.to/birajdar/platform-thread-vs-virtual-thread-in-java-16n7</link>
      <guid>https://dev.to/birajdar/platform-thread-vs-virtual-thread-in-java-16n7</guid>
      <description>&lt;p&gt;For a long time, I was confident that I understood how concurrency worked in Java.&lt;/p&gt;

&lt;p&gt;Create a thread. &lt;br&gt;
Start it. &lt;br&gt;
Join it.&lt;br&gt;
After that, a thread pool handled the growing workload.&lt;/p&gt;

&lt;p&gt;Simple… right?&lt;/p&gt;

&lt;p&gt;Then I started reading about Virtual Threads in Java (Project Loom), and suddenly I realized — we’ve been living with limitations we just accepted as “normal”.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;This post is my attempt to explain:&lt;/u&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Platform Threads are&lt;/li&gt;
&lt;li&gt;What problems virtual threads solve&lt;/li&gt;
&lt;li&gt;How they differ&lt;/li&gt;
&lt;li&gt;And when it makes sense to use one over the other&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;not like documentation, but like how I actually understood it.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;1. Platform Threads&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In Java, the classic thread model is called a Platform Thread. These are traditional threads backed directly by the Operating System.&lt;/p&gt;

&lt;p&gt;Every Java developer starts with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;doWork&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Starts a new platform thread&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It feels powerful at first. You’re doing real parallel work. Actual multitasking.&lt;/p&gt;

&lt;p&gt;But then the cracks appear.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   Creating too many threads? ❌ App slows down.&lt;/li&gt;
&lt;li&gt;   Thousands of requests? ❌ Thread pool exhausted.&lt;/li&gt;
&lt;li&gt;   Blocking I/O? ❌ Threads just sit there doing nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why...?&lt;/p&gt;

&lt;p&gt;Because platform threads are directly mapped to OS threads.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One Java thread = one OS thread.&lt;/li&gt;
&lt;li&gt;OS threads are expensive.&lt;/li&gt;
&lt;li&gt;Each thread has a big stack and scheduling cost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So instead of writing simple code, we started doing tricks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fixed-size thread pools.&lt;/li&gt;
&lt;li&gt;Async APIs.&lt;/li&gt;
&lt;li&gt;Reactive programming.&lt;/li&gt;
&lt;li&gt;Complicated execution models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not because we wanted to — but because threads didn’t scale.&lt;/p&gt;

&lt;p&gt;The Question That Couldn’t Be Ignored&lt;br&gt;
If a thread is waiting for I/O, why is it still blocking an OS thread?&lt;/p&gt;

&lt;p&gt;During I/O operations, a thread:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isn’t doing any computation.&lt;/li&gt;
&lt;li&gt;isn’t using the CPU.&lt;/li&gt;
&lt;li&gt;is simply waiting for an external operation to complete.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So why is it treated like a scarce system resource?&lt;br&gt;
If the thread isn’t actively running, why should it continue holding an OS thread?&lt;/p&gt;

&lt;p&gt;That question is exactly where Virtual Threads come in.&lt;/p&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;2. Virtual Threads — Lightweight &amp;amp; Scalable&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Virtual threads are super-light, JVM-managed threads that let us handle millions of concurrent tasks without worrying about OS thread overhead.&lt;/p&gt;

&lt;p&gt;Creating one is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startVirtualThread&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;doWork&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same programming model.&lt;/li&gt;
&lt;li&gt;Same blocking style.&lt;/li&gt;
&lt;li&gt;Totally different execution story.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Virtual threads are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Created by the JVM&lt;/li&gt;
&lt;li&gt;Not permanently attached to OS threads&lt;/li&gt;
&lt;li&gt;Extremely cheap compared to platform threads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that changes everything.&lt;/p&gt;

&lt;p&gt;What Actually Happens?&lt;br&gt;
Virtual threads run on top of platform threads, called carrier threads.&lt;/p&gt;

&lt;p&gt;When a virtual thread:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;is running -&amp;gt; it’s mounted on a carrier thread.&lt;/li&gt;
&lt;li&gt;hits a blocking operation -&amp;gt; it gets parked.&lt;/li&gt;
&lt;li&gt;is unblocked -&amp;gt; it resumes on any available carrier thread.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So instead of blocking an OS thread while waiting, the JVM simply moves on.&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Platform threads block the system.&lt;/li&gt;
&lt;li&gt;Virtual threads block only themselves.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This solves the core scalability problem of platform threads.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blocking I/O no longer wastes OS threads.&lt;/li&gt;
&lt;li&gt;Thread pools stop being a hard limit.&lt;/li&gt;
&lt;li&gt;Simple blocking code can scale to massive concurrency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Virtual threads don’t remove blocking — they remove the cost of blocking.&lt;/p&gt;

&lt;p&gt;That’s the breakthrough.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. How They Differ?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This difference is easier to see in the diagram below - notice what happens during blocking.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2fn1lui0ybdupmqi45e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2fn1lui0ybdupmqi45e.png" alt=" " width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;4. When it makes sense to use one over the other?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Virtual threads are a great default for most modern applications — especially when the workload is I/O-heavy.&lt;/p&gt;

&lt;p&gt;They make sense when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handling many concurrent requests&lt;/li&gt;
&lt;li&gt;Waiting on databases, APIs, or file systems&lt;/li&gt;
&lt;li&gt;Building web servers, APIs, or microservices&lt;/li&gt;
&lt;li&gt;Wanting simple, blocking code that still scales&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Platform threads still matter, though.&lt;/p&gt;

&lt;p&gt;They make sense when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The work is CPU-intensive&lt;/li&gt;
&lt;li&gt;Threads need tight interaction with the OS&lt;/li&gt;
&lt;li&gt;Thread pinning or native calls are involved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, it comes down to this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use virtual threads for waiting.&lt;/li&gt;
&lt;li&gt;Use platform threads for computing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That rule alone covers most real-world decisions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final take
&lt;/h2&gt;

&lt;p&gt;Virtual threads didn’t make Java faster.&lt;br&gt;
They just stopped punishing us for blocking.&lt;/p&gt;

&lt;p&gt;We can write simple code.&lt;br&gt;
We can wait on I/O.&lt;br&gt;
And the JVM handles the mess.&lt;/p&gt;

&lt;p&gt;No thread pool anxiety.&lt;br&gt;
No async gymnastics.&lt;br&gt;
No PhD in callbacks.&lt;/p&gt;

&lt;p&gt;Same Java.&lt;br&gt;
Same threads (mostly).&lt;br&gt;
Just way fewer headaches.&lt;/p&gt;

&lt;p&gt;That’s it.&lt;br&gt;
If this helped you even a little, hit ❤️, drop a comment, or share your thoughts below.&lt;/p&gt;

</description>
      <category>javamultithreading</category>
      <category>virtualthreads</category>
      <category>threadconcurrency</category>
      <category>programming</category>
    </item>
    <item>
      <title>I N T E R F A C E (JAVA-8)</title>
      <dc:creator>Bhagyashri Birajdar</dc:creator>
      <pubDate>Tue, 20 Feb 2024 07:34:30 +0000</pubDate>
      <link>https://dev.to/birajdar/i-n-t-e-r-f-a-c-e-java-8-98n</link>
      <guid>https://dev.to/birajdar/i-n-t-e-r-f-a-c-e-java-8-98n</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;I n t r o d u c t i o n&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Java interfaces are a fundamental concept in the world of Java programming.We'll start by understanding what interfaces are? and then explore their syntax, usage and the various features of interfaces.&lt;br&gt;
Additionally, we'll talk about the evolution of interfaces in Java. It will cover how interfaces have developed and improved over the years, starting from their basic form in the earlier versions of Java to the enhancements and changes introduced in Java 8.&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;u&gt; Why Interfaces Were Introduced in Java? &lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;Interfaces were introduced in Java to achieve : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;abstraction&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;multiple inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;polymorphism &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;promoting modular, flexible, and reusable code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5wsihheen8u50j6pldn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5wsihheen8u50j6pldn.jpg" alt="Image description" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;u&gt; I N T E R F A C E &lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;In Java, an interface is a blueprint of a class and is a key mechanism for achieving abstraction, that can contain only constants, method signatures, default methods, static methods, and nested types. However, it cannot contain method implementations. Instead, classes that implement interfaces&lt;br&gt;
must have to provide the method implementations.&lt;/p&gt;

&lt;p&gt;Java Interface also represents the IS-A relationship.&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;u&gt; The relationship between classes and interfaces &lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;A class can extend another class similar to this an interface can extend another interface using &lt;code&gt;extends&lt;/code&gt; keyword and a class can implement another interface using &lt;code&gt;implements&lt;/code&gt; keyword but vice-versa is not allowed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju2ufx80b0j56x4wa3hh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fju2ufx80b0j56x4wa3hh.jpg" alt="Image description" width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;u&gt; How to declare interface in java? &lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;Use the &lt;code&gt;interface&lt;/code&gt; keyword followed by the name of the interface. &lt;br&gt;
Inside the interface, define method signatures. These are declarations of methods with the empty body. &lt;br&gt;
By default, methods in interfaces are abstract and all the fields are public, static and final.&lt;br&gt;
A class that implements an interface must implement all the methods declared in the interface using &lt;code&gt;implements&lt;/code&gt; keyword. &lt;/p&gt;

&lt;p&gt;use the following syntax to define interface in java :&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;u&gt;S y n t a x &lt;/u&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;interface_name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// declare constant fields  &lt;/span&gt;
        &lt;span class="c1"&gt;// declare methods that abstract   &lt;/span&gt;
        &lt;span class="c1"&gt;// by default.  &lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;u&gt;Java Interface Example&lt;/u&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;play&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// public, static and final&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;cricket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;football&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this example, The play interface declares a constant 'a', which is public, static, and final, along with abstract methods cricket() and football().&lt;br&gt;
Any class that implements the play interface must provide implementations for these methods.&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;u&gt;Implementing Interfaces&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;To implement the play interface, you'll need to create a class that implements it and provide concrete implementations for the abstract methods.&lt;br&gt;
A class uses the &lt;code&gt;implements&lt;/code&gt; keyword to implement an interface . Let's say we have a class named "student" that implements the "play" interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;student&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;cricket&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"student is playing cricket"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;football&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"student is playing football"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By implementing the "play" interface, the "student" class must have to provide implementations for the cricket() and football() methods.&lt;/p&gt;




&lt;p&gt;In Java, you cannot instantiate an interface directly; instead, you create instances of classes that implement the interface, allowing you to create references of the interface type and assign them to objects of implementing classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Instantiating the student class&lt;/span&gt;
        &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cricket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: student is playing cricket&lt;/span&gt;
        &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;football&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: student is playing football&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, student is a reference variable of type play, which is assigned an object of type student. Through this reference variable, you can call the cricket() and football() methods defined in the play interface, and the implementations provided in the student class will be executed.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;u&gt;New features added in interface from java 8&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;Java 8 introduced default and static methods as well as functional interfaces to enhance the versatility and power of interfaces.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Default Methods :
&lt;/h4&gt;

&lt;p&gt;Interfaces can now have default methods, which provide a default implementation for a method. Default methods are declared using the &lt;code&gt;default&lt;/code&gt; keyword and can be overridden by implementing classes if needed. This allows interfaces to evolve without breaking existing implementations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;play&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// public, static and final&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;cricket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;football&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;

    &lt;span class="c1"&gt;// Default method&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;hockey&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" student playing hockey"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above modification, I've added a default method named hockey to the play interface, which prints "student playing hockey" when invoked.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Satic Methods :
&lt;/h4&gt;

&lt;p&gt;Java 8 introduced static methods in interfaces. These methods are declared using the &lt;code&gt;static&lt;/code&gt; keyword and can be called directly on the interface, without requiring an instance of a class. Static methods in interfaces provide utility methods that are closely related to the interface's purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;play&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// public, static and final&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;cricket&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;football&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;

    &lt;span class="c1"&gt;// Static method&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;tennis&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"student playing tennis"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above modification, I've added a static method named tennis to the play interface, which prints "student playing tennis" when invoked. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;3. Functional Interface&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;Java 8 introduced the concept of functional interfaces, which are interfaces that contain exactly one abstract method and may contain multiple default or static methods.&lt;br&gt;
Functional interfaces enable the use of lambda expressions, providing a concise way to express instances of single-method interfaces. The @FunctionalInterface annotation was introduced to denote such interfaces, allowing the compiler to enforce the single abstract method requirement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;   
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Play&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
     &lt;span class="c1"&gt;// Abstract methods                     &lt;/span&gt;
        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;playGame&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;                   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Built-in Java Functional Interfaces
&lt;/h4&gt;

&lt;p&gt;Runnable - This interface only contains the run() method.&lt;/p&gt;

&lt;p&gt;Comparable - This interface only contains the compareTo()&lt;br&gt;&lt;br&gt;
method.&lt;/p&gt;

&lt;p&gt;ActionListener – This interface only contains the actionPerformed() method.   &lt;/p&gt;

&lt;p&gt;Callable – This interface only contains the call() method.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;Important points about interface&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffeaqxn9w78peug1oofll.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffeaqxn9w78peug1oofll.jpg" alt="Image description" width="800" height="845"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for taking the time to engage with my document! i hope that this overview has provided you with a clearer understanding of Java interfaces.&lt;/p&gt;

&lt;p&gt;In our next blog post, we'll explore another method of achieving abstraction in Java: abstract classes. We'll also examine how abstract classes differ from interfaces, exploring their distinct approaches to abstraction. Stay tuned to enhance your understanding of these fundamental  concepts of Java!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Records in java</title>
      <dc:creator>Bhagyashri Birajdar</dc:creator>
      <pubDate>Sun, 19 Nov 2023 09:27:37 +0000</pubDate>
      <link>https://dev.to/birajdar/records-in-java-2cd6</link>
      <guid>https://dev.to/birajdar/records-in-java-2cd6</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;Introduction&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;In JAVA, record class is nothing but &lt;em&gt;(immutable data)&lt;/em&gt; data carrying class between modules with the clear intention to reduce boilerplate code and achieve maximum efficiency.&lt;br&gt;
Java Records is a feature introduced as a preview feature in Java 14 and became a standard feature in Java 16.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Reason behind why records introduced?&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Before Java had record classes, if you wanted to create a class to hold data (like a person's name and id), you had to write a lot of repetitive code. This included making a constructor, getter and setter methods and if you want to print the contents of its objects as a string, we would need to override methods such as equals(), hashCode(), and toString().&lt;/p&gt;

&lt;p&gt;&lt;u&gt;let's understand above statement with example :-&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Take a class 'person' with attributes for name and ID, providing methods to access these attributes and overriding methods like toString, hashCode, and equals for proper object representation, hashing, and value-based equality comparison respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Objects&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// assinging values&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//for accesing values &lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getname&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getid&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// accessing content of object as string&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"person{"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"name="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", id='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'}'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// to compare the value instead of mamory location&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="n"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 'Data_carry' class for demonstrating object creation and comparison using these overridden methods to check if two 'person' objects hold the same values, not just memory locations, facilitating value-based comparisons in Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Data_carry&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"birajdar"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"birajdar"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getname&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\n"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"id = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getid&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;see, lot's of code we should write with simple java class to hold/carry some data.&lt;br&gt;
To overcome this hadeche, JAVA introduced Recored class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;how to create record class?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Record class declaration part is slightly different from
typical class declaration in java. means it quite similar to 
funtion.&lt;/li&gt;
&lt;li&gt;Record class is create by using "record" keyword followed by
"record_name" in its declaration.&lt;/li&gt;
&lt;li&gt;In parentheses, parameters of this record class contain a
comma separated list of fields.&lt;/li&gt;
&lt;li&gt;Body of the record class is optional.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;syntax :-&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;record_name&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="c1"&gt;// optional&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;how record class works?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Record class reduce boilerplate code by automatically compiler generate standard implementations for common tasks like constructors, getter(), setter() methods to access data, equals(), hashCode(), and toString() methods to access content of object as string without any intervention of the programmer. This allows developers to focus on defining the data fields and their behavior.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;let's understand record class with example :-&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;){}&lt;/span&gt;    &lt;span class="c1"&gt;// record class&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"birajdar"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"birajdar"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"\n"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"age = "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;


    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Explanation :-&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;In the above example a class 'Person' with name and ID attributes is created, showcasing object instantiation, attribute access, printing object details, and checking for equality in a concise and readable manner.&lt;/p&gt;

&lt;p&gt;As we can see, we can reduce a complete class with a single line of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Important points about records&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) record class is immutable, meaning that once an object is created, its state cannot be changed.&lt;br&gt;
2) A record class is instantiated by the new keyword, just like creating any other objects in Java.&lt;br&gt;
3) records are implicitly final and cannot be extended by another class.&lt;br&gt;
4) records cannot explicitly define additional instance variables beyond the automatically generated private final fields associated with their components.&lt;br&gt;
5) Any other fields, except the list of components, must be declared static.&lt;br&gt;
6) A record can implement one or more interface.&lt;br&gt;
7) Records are serializable.&lt;/p&gt;

&lt;p&gt;that's all about record now we will ready to learn, how record class and sealed interface works together? &lt;em&gt;(prerequisite :- &lt;a href="https://dev.to/birajdar/can-sealed-interfaces-in-java-improve-code-security-828"&gt;Sealed Interface&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Sealed interface with Record classes&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand how record classes and sealed interfaces can work together, let's expand on the concept using an example.&lt;/p&gt;

&lt;p&gt;let's consider an example where we create sealed interface "company" and then create record classes such as "manager" &amp;amp; "developer" that implement this interface &amp;amp; provide their own implementations for the info() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;
&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;company&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Manager&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;developer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;work&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Manager&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;company&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Manager "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" (age: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;") is managing teams."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;developer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;company&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

     &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Developer"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" (age: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;") is working on coding tasks."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SealedInterface_RecordClass&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;company&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Manager&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"paradox"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;company&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;developer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"max"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Advantages of Sealed Interfaces with Record Classes&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) Structural Consistency: Sealed interfaces maintain a controlled hierarchy by defining which classes can implement them.&lt;/p&gt;

&lt;p&gt;2) Reduced Boilerplate: Records reduce repetitive code by and enhancing code readability.&lt;/p&gt;

&lt;p&gt;3) Enhanced Readability: Sealed interfaces clarify class relationships, while records simplify the creation of immutable data models.&lt;/p&gt;

&lt;p&gt;Thanks 🙏 for reading! Hopefully, this article helps you to understand Record class in Java. Please submit any suggestions or comments you may have in the comment section of this article.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Can Sealed Interfaces in Java Improve Code Security?</title>
      <dc:creator>Bhagyashri Birajdar</dc:creator>
      <pubDate>Fri, 06 Oct 2023 16:42:11 +0000</pubDate>
      <link>https://dev.to/birajdar/can-sealed-interfaces-in-java-improve-code-security-828</link>
      <guid>https://dev.to/birajdar/can-sealed-interfaces-in-java-improve-code-security-828</guid>
      <description>&lt;p&gt;In the field of software development, security and robustness are of utmost importance to protect against potential vulnerabilities.&lt;/p&gt;

&lt;p&gt;Sealed interfaces in Java17 are a simple &amp;amp; powerful way to enhance code security.&lt;/p&gt;

&lt;p&gt;In our previous blog post, We covered the concept of &lt;a href="https://dev.to/birajdar/java17-sealed-classes-4pl4"&gt;Sealed Classes&lt;/a&gt;. Now, it's time to turn our attention towards sealed interfaces &amp;amp; how they improve code security. But before that, you must have an idea about what an interface is.&lt;/p&gt;

&lt;p&gt;In Java 17, sealed classes and sealed interfaces share similar rules and behavior.&lt;/p&gt;

&lt;p&gt;Like sealed classes, to seal an interface, add the &lt;code&gt;sealed&lt;/code&gt; keyword to it's declaration. After "extends" statement, include a &lt;code&gt;permits&lt;/code&gt; clause &lt;em&gt;(If there are no "extends" statements, you can skip that part)&lt;/em&gt;. &lt;code&gt;permits&lt;/code&gt; clause tells which classes are allowed to implement this sealed interface and which interfaces can extend it. &lt;/p&gt;

&lt;p&gt;When an interface is marked as sealed, any class or interface attempting to implement or extend it must be listed in the permits clause of the sealed interface. Otherwise, it will result in a compilation error.&lt;/p&gt;

&lt;p&gt;This way, you control who can work with your interface, ensuring everything stays well-organized and secure.&lt;/p&gt;

&lt;h2&gt;
  
  
  syntax
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Interface_Name&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Subinterface1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Subinterface2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Interface members and methods&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Color interface"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Java code defines a sealed interface named "Color" that permits implementation by the interface "Red" and the class "Green". It includes a default "display()" method which prints "This is Color interface" when called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;non&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Red interface"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a non-sealed interface named "Red" which extends the "Color" interface. It includes a default method "show()" that prints "This is Red interface" when called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;play&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Green class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code defines a final class named "Green" which implements the interface "Color". It includes a method "play()" that prints "This is Green class". The use of final means that "Green" class cannot be subclassed or extended further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Execution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is Color interface
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Records can be used with Sealed Interfaces. In our next blog post, we'll explore what a record is and how records and sealed interfaces work together. Gear up for an exciting exploration of these Java features! Stay tuned!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>java</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Java17 sealed classes</title>
      <dc:creator>Bhagyashri Birajdar</dc:creator>
      <pubDate>Sat, 23 Sep 2023 10:14:00 +0000</pubDate>
      <link>https://dev.to/birajdar/java17-sealed-classes-4pl4</link>
      <guid>https://dev.to/birajdar/java17-sealed-classes-4pl4</guid>
      <description>&lt;h2&gt;
  
  
  Are Java 17 Sealed Classes a Game-Changer for Inheritance Control?
&lt;/h2&gt;




&lt;p&gt;&lt;u&gt;&lt;strong&gt;Introduction:-&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17. &lt;br&gt;
In this complete blog tutorial, we'll see what sealed classes are and why they're useful. But first, let's start with the basic definition of a sealed class.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Sealed class:-&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;"Sealed class offer a powerful mechanism to restrict the inheritance hierarchy. "&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Which means, When you want's inheritance but not all classes can do it, their should be a limited classes which you want's to be inherited. then you can do this by using sealed class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Declaration of sealed class is not much complicated. simply you just need to add the &lt;code&gt;sealed&lt;/code&gt; keyword to it's declaration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the class declaration add &lt;code&gt;permits clause&lt;/code&gt; to give the permission to those sub-classes which you want's to be inherited.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only permitted classes can have the access to extends the sealed class.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Syntax :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Class_Name&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Subclass1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Subclass2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Class members and methods&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ex :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Blue&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Color class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;in the above example we can see that the &lt;code&gt;Color&lt;/code&gt; class is a sealed class that give the permission to &lt;code&gt;Red&lt;/code&gt; &amp;amp; &lt;code&gt;Green&lt;/code&gt; classes to extends it. If any other class tries to extend it, then it will give a compiler error.&lt;/li&gt;
&lt;li&gt;A class that extends a sealed class must have either &lt;code&gt;final&lt;/code&gt; , &lt;code&gt;sealed&lt;/code&gt; or &lt;code&gt;non-sealed&lt;/code&gt; keyword in its declaration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;1) &lt;u&gt;&lt;strong&gt;sealed&lt;/strong&gt;&lt;/u&gt; :-&lt;br&gt;
sealed class can only be extended by it's permitted sub-classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Red class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code defines a sealed class "Red" extending it's super-class "Color" and permitting only class "R", with a display method printing "This is Red class".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is R class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code defines a final class "R" that extends the sealed class "Red" and overrides the display method to print "This is R class".&lt;/p&gt;

&lt;p&gt;2) &lt;u&gt;&lt;strong&gt;non-sealed&lt;/strong&gt; &lt;/u&gt;:-&lt;br&gt;
non-sealed class can be extends by any sub-class. a sealed class cannot prevent its permitted sub-classes from doing this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;non&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Green class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code defines a non-sealed class "Green" that extends it's super-class "Color" and contains a display method which prints "This is Green class".&lt;/p&gt;

&lt;p&gt;3) &lt;u&gt;&lt;strong&gt;final&lt;/strong&gt; &lt;/u&gt;:-&lt;br&gt;
final class can't be inherited further.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blue&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is Blue class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code defines a final class "Blue" that extends it's super-class "Color" and contains a display method which prints "This is Blue class". This means that the class "Blue" cannot be subclassed further.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Code Execution&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;   
        &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Red&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Green&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;obj3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Blue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;obj1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;obj2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;obj3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;Red&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;display&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Output&lt;/u&gt; :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is Red class
This is Green class
This is Blue class
This is R class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;strong&gt;features&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Sealed classes helps in creating a finite set of classes in inheritance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sealed classes specify which classes can extend them, providing a clear hierarchy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sealed classes ensure that only permitted sub-classes are created, avoiding unexpected behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Useful for creating frameworks, allowing developers to dictate which classes users can extend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Especially beneficial in large projects, it prevents unintentional changes to important classes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note:-&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Sealed class must specify it's permitted sub-class using permits clause.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Sealed class must have sub-classes.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Only the listed sub-classes are allowed to extend the sealed class.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Permitted sub-classes must be in same package.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;- Permitted sub-classes should extends their sealed super class directly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;sealed classes in Java provide a powerful tool for controlling class hierarchies. They allow us to explicitly specify which classes can extend a particular class, offering a clear and organized structure for our code. This feature is valuable in preventing unintended subclasses and maintaining code integrity.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Hopefully, this article helps you to understand sealed class in Java17. Please submit any suggestions or comments you may have in the comment section of this article and don't forget to hit the ❤️ like. &lt;br&gt;
Thank you.😀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/birajdar/can-sealed-interfaces-in-java-improve-code-security-828"&gt;sealed interface&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>java17</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
