<?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: Mukesh</title>
    <description>The latest articles on DEV Community by Mukesh (@mukeshb).</description>
    <link>https://dev.to/mukeshb</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%2F1705706%2F16fc635e-27dc-4ab8-9f8d-258e0950f73a.png</url>
      <title>DEV Community: Mukesh</title>
      <link>https://dev.to/mukeshb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mukeshb"/>
    <language>en</language>
    <item>
      <title>Abstraction in Java Explained: Abstract Classes vs Interfaces</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Mon, 28 Jul 2025 18:27:27 +0000</pubDate>
      <link>https://dev.to/mukeshb/abstraction-in-java-explained-abstract-classes-vs-interfaces-3mn9</link>
      <guid>https://dev.to/mukeshb/abstraction-in-java-explained-abstract-classes-vs-interfaces-3mn9</guid>
      <description>&lt;h2&gt;
  
  
  Understanding Abstraction in Java
&lt;/h2&gt;

&lt;p&gt;In the world of object-oriented programming, &lt;strong&gt;abstraction&lt;/strong&gt; plays a vital role in simplifying complex systems. It allows developers to focus on what an object does rather than how it does it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Abstraction?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Abstraction&lt;/strong&gt; is the process of hiding internal implementation details and exposing only the essential functionality of an object. It helps reduce programming complexity and effort by providing a cleaner and more focused interface.&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;br&gt;
Abstraction lets you focus on the &lt;strong&gt;"what"&lt;/strong&gt; instead of the &lt;strong&gt;"how"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, when you drive a car, you interact with the steering wheel, pedals, and dashboard. You don’t need to know how the engine works internally—that’s abstraction.&lt;/p&gt;
&lt;h3&gt;
  
  
  Achieving Abstraction in Java
&lt;/h3&gt;

&lt;p&gt;Java provides two primary ways to achieve abstraction:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. &lt;strong&gt;Abstract Class&lt;/strong&gt; (0% to 100% Abstraction)
&lt;/h4&gt;

&lt;p&gt;An &lt;strong&gt;abstract class&lt;/strong&gt; in Java is a class that cannot be instantiated and may contain abstract methods (without a body) as well as concrete methods (with a body).&lt;/p&gt;
&lt;h5&gt;
  
  
  Key Points:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Declared using the &lt;code&gt;abstract&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;Can have both &lt;strong&gt;abstract&lt;/strong&gt; and &lt;strong&gt;non-abstract&lt;/strong&gt; methods.&lt;/li&gt;
&lt;li&gt;Can include &lt;strong&gt;constructors&lt;/strong&gt;, &lt;strong&gt;static&lt;/strong&gt;, and &lt;strong&gt;final&lt;/strong&gt; methods.&lt;/li&gt;
&lt;li&gt;Meant to be &lt;strong&gt;extended&lt;/strong&gt; by subclasses which implement abstract methods.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&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;stop&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;"Vehicle stopped."&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;h5&gt;
  
  
  Abstract Method:
&lt;/h5&gt;

&lt;p&gt;A method without implementation, declared using the &lt;code&gt;abstract&lt;/code&gt; keyword.&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;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printStatus&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// no body&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a class contains even one abstract method, class must be declared as &lt;code&gt;abstract&lt;/code&gt;, or it will result in a compile-time error.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Interface&lt;/strong&gt; (100% Abstraction)
&lt;/h4&gt;

&lt;p&gt;An &lt;strong&gt;interface&lt;/strong&gt; in Java is like contract or blueprint. It defines what should be done, but not how.&lt;/p&gt;

&lt;h5&gt;
  
  
  Key Points:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Contains only &lt;strong&gt;abstract methods&lt;/strong&gt; and &lt;strong&gt;static constants&lt;/strong&gt; (before Java 8).&lt;/li&gt;
&lt;li&gt;Cannot have method bodies (until Java 8).&lt;/li&gt;
&lt;li&gt;Supports &lt;strong&gt;multiple inheritance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Promotes &lt;strong&gt;loose coupling&lt;/strong&gt; between components.&lt;/li&gt;
&lt;li&gt;Interfaces represent the &lt;strong&gt;"IS-A"&lt;/strong&gt; relationship.
&lt;/li&gt;
&lt;/ul&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="nc"&gt;Drawable&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;draw&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;h5&gt;
  
  
  Enhancements Since Java 8 and 9:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Java 8&lt;/strong&gt;: Interfaces can have &lt;strong&gt;default&lt;/strong&gt; and &lt;strong&gt;static&lt;/strong&gt; methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Java 9&lt;/strong&gt;: Interfaces can also have &lt;strong&gt;private&lt;/strong&gt; methods.
&lt;/li&gt;
&lt;/ul&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="nc"&gt;Shape&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;log&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;"Logging Shape"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&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;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;"Static method in 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;h3&gt;
  
  
  When to Use What?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;strong&gt;abstract classes&lt;/strong&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want to provide common functionality to all subclasses.&lt;/li&gt;
&lt;li&gt;You want to define non-static or non-final fields.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Use &lt;strong&gt;interfaces&lt;/strong&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to implement &lt;strong&gt;multiple inheritances&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You want to define a &lt;strong&gt;contract&lt;/strong&gt; that multiple unrelated classes can implement.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Abstraction is one of the four pillars of object-oriented programming. Whether you choose &lt;strong&gt;abstract classes&lt;/strong&gt; or &lt;strong&gt;interfaces&lt;/strong&gt; depends on your design needs, but both serve the same fundamental purpose: &lt;strong&gt;to hide complexity and expose simplicity&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Understanding Covariant Return Types in Java</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sat, 19 Jul 2025 18:50:31 +0000</pubDate>
      <link>https://dev.to/mukeshb/understanding-covariant-return-types-in-java-4l2l</link>
      <guid>https://dev.to/mukeshb/understanding-covariant-return-types-in-java-4l2l</guid>
      <description>&lt;p&gt;In Java, &lt;strong&gt;polymorphism&lt;/strong&gt; allows method to be overridden in subclass with different implementation. However, when overriding methods, the return type of the method must usually match exactly. This is where &lt;strong&gt;Covariant Return Type&lt;/strong&gt; comes into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is Covariant Return Type?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;covariant return type&lt;/strong&gt; specifies that the return type of overridden method in subclass may vary in the same direction as the subclass. In simple terms, if the superclass method returns a type &lt;code&gt;A&lt;/code&gt;, then the overridden method in the subclass can return &lt;code&gt;B&lt;/code&gt; (where &lt;code&gt;B&lt;/code&gt; is a subclass of &lt;code&gt;A&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This feature was introduced in &lt;strong&gt;Java 5&lt;/strong&gt; and helps make code cleaner, safer, and more readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example of Covariant Return Type in Java&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="nf"&gt;get&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="k"&gt;this&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;class&lt;/span&gt; &lt;span class="nc"&gt;B1&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="nd"&gt;@Override&lt;/span&gt;  
    &lt;span class="no"&gt;B1&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// Return type changed to subclass type&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&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;message&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;"Welcome to covariant return type"&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;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="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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;B1&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;message&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;h3&gt;
  
  
  &lt;strong&gt;Output&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Welcome to covariant return type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In the above example:

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;get()&lt;/code&gt; method in class &lt;strong&gt;A&lt;/strong&gt; returns an object of type &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;get()&lt;/code&gt; method in subclass &lt;strong&gt;B1&lt;/strong&gt; overrides this method but changes the return type to &lt;code&gt;B1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;This is valid because &lt;code&gt;B1&lt;/code&gt; &lt;strong&gt;is-a&lt;/strong&gt; &lt;code&gt;A&lt;/code&gt; (inheritance).&lt;/li&gt;

&lt;li&gt;Both methods are considered &lt;strong&gt;overridden&lt;/strong&gt;, even though they return different types.
This behavior is called &lt;strong&gt;Covariant Return Type&lt;/strong&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why is Covariant Return Type Useful?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before covariant return types, we had to perform explicit type casting to access subclass-specific methods after overriding. &lt;/p&gt;

&lt;p&gt;With covariant return types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Avoids confusing type casts&lt;/strong&gt; in class hierarchies.&lt;/li&gt;
&lt;li&gt;Makes the code &lt;strong&gt;more readable and maintainable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In method overriding, it provides the &lt;strong&gt;liberty to return more specific types&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prevents &lt;code&gt;ClassCastException&lt;/code&gt; at runtime&lt;/strong&gt; when handling returned objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Advantages&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Code becomes &lt;strong&gt;cleaner&lt;/strong&gt; and easier to understand.&lt;/li&gt;
&lt;li&gt;Reduces boilerplate and explicit &lt;strong&gt;type casting&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Improves &lt;strong&gt;type safety&lt;/strong&gt; at compile time.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Polymorphism in Java – Understanding Compile-time and Runtime Behavior</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Thu, 17 Jul 2025 19:46:31 +0000</pubDate>
      <link>https://dev.to/mukeshb/polymorphism-in-java-understanding-compile-time-and-runtime-behavior-5f82</link>
      <guid>https://dev.to/mukeshb/polymorphism-in-java-understanding-compile-time-and-runtime-behavior-5f82</guid>
      <description>&lt;p&gt;Polymorphism in Java is one of the core concepts of Object-Oriented Programming (OOP). It allows us to perform a single action in different ways. Polymorphism means &lt;strong&gt;"many forms"&lt;/strong&gt;. In simple terms, it allows &lt;strong&gt;one interface to be used for multiple implementations&lt;/strong&gt;, making our code flexible and reusable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Types of Polymorphism in Java&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In Java, polymorphism is classified into two main types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Compile-time Polymorphism (Method Overloading)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Runtime Polymorphism (Method Overriding)&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Compile-time Polymorphism (Method Overloading)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Compile-time polymorphism occurs when the method call is resolved at &lt;strong&gt;compile time&lt;/strong&gt;. The most common example of this is &lt;strong&gt;Method Overloading&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is Method Overloading?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If a class has multiple methods with the &lt;strong&gt;same name&lt;/strong&gt; but different parameter lists (different number or type of parameters) it is called &lt;strong&gt;Method Overloading&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;add&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;a&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;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;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;Calculator&lt;/span&gt; &lt;span class="n"&gt;calc&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;Calculator&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;calc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;// calls int version&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;calc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;10.2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// calls double version&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;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increases &lt;strong&gt;readability&lt;/strong&gt; of the program&lt;/li&gt;
&lt;li&gt;Same method name with different argument list is &lt;strong&gt;easy to remember&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Ways to Achieve Overloading&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;By changing the &lt;strong&gt;number of arguments&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;By changing the &lt;strong&gt;data type of arguments&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. Runtime Polymorphism (Method Overriding)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Runtime polymorphism, also known as &lt;strong&gt;Dynamic Method Dispatch&lt;/strong&gt;, is achieved through &lt;strong&gt;Method Overriding&lt;/strong&gt;. In this case, the method call is resolved &lt;strong&gt;at runtime&lt;/strong&gt;, not at compile time.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is Method Overriding?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If a &lt;strong&gt;subclass (child class)&lt;/strong&gt; provides &lt;strong&gt;specific implementation&lt;/strong&gt; of method that is already defined in its &lt;strong&gt;parent class&lt;/strong&gt;, it is called &lt;strong&gt;Method Overriding&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rules for Overriding:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The method in the child class must have the same name, return type, and parameters as in the parent class.&lt;/li&gt;
&lt;li&gt;It requires inheritance (using &lt;code&gt;extends&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example:&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;sound&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;"Animal makes a sound"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sound&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;"Dog barks"&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;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;Animal&lt;/span&gt; &lt;span class="n"&gt;a&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;Dog&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Upcasting&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sound&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Dog barks (resolved at runtime)&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;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To provide a specific implementation in the child class&lt;/li&gt;
&lt;li&gt;To achieve runtime polymorphism.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Polymorphism makes Java programs more flexible, extensible, and easier to maintain.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Overloading&lt;/strong&gt; for compile-time decisions and code clarity.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;Overriding&lt;/strong&gt; for dynamic behavior and runtime flexibility.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Mastering Inheritance in Java: A Beginner’s Guide to Code Reusability</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sun, 13 Jul 2025 06:47:08 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-inheritance-in-java-a-beginners-guide-to-code-reusability-41fh</link>
      <guid>https://dev.to/mukeshb/mastering-inheritance-in-java-a-beginners-guide-to-code-reusability-41fh</guid>
      <description>&lt;h2&gt;
  
  
  Inheritance in Java – Reusing Code the Right Way
&lt;/h2&gt;

&lt;p&gt;Inheritance is one of the core pillars of &lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt;, and Java provides robust support for it. In simple terms, &lt;strong&gt;inheritance allows one class to acquire properties and behaviors (methods and fields) of another class&lt;/strong&gt;, promoting reusability and cleaner code design.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Inheritance?
&lt;/h3&gt;

&lt;p&gt;In Java, &lt;strong&gt;inheritance&lt;/strong&gt; means when one object (or class) &lt;strong&gt;inherits&lt;/strong&gt; or acquires all the features (fields and methods) of another object. This leads to the formation of an &lt;strong&gt;IS-A&lt;/strong&gt; relationship — for example a &lt;strong&gt;Dog IS-A Animal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This relationship is also known as a &lt;strong&gt;parent-child&lt;/strong&gt; or &lt;strong&gt;superclass-subclass&lt;/strong&gt; relationship.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;eat&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 animal eats food"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;bark&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;"Dog barks"&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;Here, &lt;code&gt;Dog&lt;/code&gt; inherits from &lt;code&gt;Animal&lt;/code&gt;, so it can &lt;code&gt;eat()&lt;/code&gt; and &lt;code&gt;bark()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use Inheritance in Java?
&lt;/h3&gt;

&lt;p&gt;Java supports inheritance to promote:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Reusability&lt;/strong&gt;&lt;br&gt;
Common logic can be written once in a parent class and reused in child classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Method Overriding&lt;/strong&gt;&lt;br&gt;
Child classes can override parent class methods to implement &lt;strong&gt;runtime polymorphism&lt;/strong&gt;, enabling flexible behavior at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Syntax: Using the &lt;code&gt;extends&lt;/code&gt; Keyword
&lt;/h3&gt;

&lt;p&gt;Inheritance in Java is enabled using the &lt;code&gt;extends&lt;/code&gt; keyword.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// additional fields and methods&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types of Inheritance in Java
&lt;/h3&gt;

&lt;p&gt;Java supports several forms of inheritance:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Single Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;One class inherits from one other 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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;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;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;show&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;h4&gt;
  
  
  2. &lt;strong&gt;Multilevel Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A chain of inheritance.&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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;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;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;show&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;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;B&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;print&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;h4&gt;
  
  
  3. &lt;strong&gt;Hierarchical Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Multiple classes inherit from a single parent 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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;greet&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;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What about Multiple Inheritance?
&lt;/h3&gt;

&lt;p&gt;Java &lt;strong&gt;does not support multiple inheritance with classes&lt;/strong&gt; to avoid ambiguity issues (like the Diamond Problem). But it does support multiple through &lt;strong&gt;interfaces&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Printable&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;print&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Showable&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;show&lt;/span&gt;&lt;span class="o"&gt;();&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;Demo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Printable&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Showable&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;print&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;show&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;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Inheritance makes Java code &lt;strong&gt;cleaner, reusable, and extensible&lt;/strong&gt;. It's a powerful feature that, when used wisely, helps build robust OOP-based systems.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Mastering Inheritance in Java: A Beginner’s Guide to Code Reusability</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sun, 13 Jul 2025 06:47:08 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-inheritance-in-java-a-beginners-guide-to-code-reusability-1ihl</link>
      <guid>https://dev.to/mukeshb/mastering-inheritance-in-java-a-beginners-guide-to-code-reusability-1ihl</guid>
      <description>&lt;h2&gt;
  
  
  Inheritance in Java – Reusing Code the Right Way
&lt;/h2&gt;

&lt;p&gt;Inheritance is one of the core pillars of &lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt;, and Java provides robust support for it. In simple terms, &lt;strong&gt;inheritance allows one class to acquire properties and behaviors (methods and fields) of another class&lt;/strong&gt;, promoting reusability and cleaner code design.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Inheritance?
&lt;/h3&gt;

&lt;p&gt;In Java, &lt;strong&gt;inheritance&lt;/strong&gt; means when one object (or class) &lt;strong&gt;inherits&lt;/strong&gt; or acquires all the features (fields and methods) of another object. This leads to the formation of an &lt;strong&gt;IS-A&lt;/strong&gt; relationship — for example a &lt;strong&gt;Dog IS-A Animal&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This relationship is also known as a &lt;strong&gt;parent-child&lt;/strong&gt; or &lt;strong&gt;superclass-subclass&lt;/strong&gt; relationship.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;eat&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 animal eats food"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;bark&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;"Dog barks"&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;Here, &lt;code&gt;Dog&lt;/code&gt; inherits from &lt;code&gt;Animal&lt;/code&gt;, so it can &lt;code&gt;eat()&lt;/code&gt; and &lt;code&gt;bark()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use Inheritance in Java?
&lt;/h3&gt;

&lt;p&gt;Java supports inheritance to promote:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Reusability&lt;/strong&gt;&lt;br&gt;
Common logic can be written once in a parent class and reused in child classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Method Overriding&lt;/strong&gt;&lt;br&gt;
Child classes can override parent class methods to implement &lt;strong&gt;runtime polymorphism&lt;/strong&gt;, enabling flexible behavior at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Syntax: Using the &lt;code&gt;extends&lt;/code&gt; Keyword
&lt;/h3&gt;

&lt;p&gt;Inheritance in Java is enabled using the &lt;code&gt;extends&lt;/code&gt; keyword.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// additional fields and methods&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types of Inheritance in Java
&lt;/h3&gt;

&lt;p&gt;Java supports several forms of inheritance:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Single Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;One class inherits from one other 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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;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;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;show&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;h4&gt;
  
  
  2. &lt;strong&gt;Multilevel Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A chain of inheritance.&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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;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;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;show&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;class&lt;/span&gt; &lt;span class="nc"&gt;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;B&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;print&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;h4&gt;
  
  
  3. &lt;strong&gt;Hierarchical Inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Multiple classes inherit from a single parent 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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;greet&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;class&lt;/span&gt; &lt;span class="nc"&gt;B&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&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;C&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What about Multiple Inheritance?
&lt;/h3&gt;

&lt;p&gt;Java &lt;strong&gt;does not support multiple inheritance with classes&lt;/strong&gt; to avoid ambiguity issues (like the Diamond Problem). But it does support multiple through &lt;strong&gt;interfaces&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Printable&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;print&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Showable&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;show&lt;/span&gt;&lt;span class="o"&gt;();&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;Demo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Printable&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Showable&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;print&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;show&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;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Inheritance makes Java code &lt;strong&gt;cleaner, reusable, and extensible&lt;/strong&gt;. It's a powerful feature that, when used wisely, helps build robust OOP-based systems.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Mastering super Keyword in Java: A Beginner's Guide to Inheritance</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Tue, 08 Jul 2025 18:36:55 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-super-keyword-in-java-a-beginners-guide-to-inheritance-1lob</link>
      <guid>https://dev.to/mukeshb/mastering-super-keyword-in-java-a-beginners-guide-to-inheritance-1lob</guid>
      <description>&lt;h2&gt;
  
  
  Understanding the &lt;code&gt;super&lt;/code&gt; Keyword in Java
&lt;/h2&gt;

&lt;p&gt;When working with &lt;strong&gt;inheritance in Java&lt;/strong&gt;, &lt;code&gt;super&lt;/code&gt; keyword plays key role in accessing members (variables, methods, and constructors) of parent class. It helps avoid confusion when subclass and superclass share similar names for methods or variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;super&lt;/code&gt; in Java?
&lt;/h3&gt;

&lt;p&gt;In Java, &lt;code&gt;super&lt;/code&gt; is &lt;strong&gt;reference variable&lt;/strong&gt; used to refer to &lt;strong&gt;immediate parent class object&lt;/strong&gt;. Whenever you create an instance of subclass, an instance of its superclass is also created, and &lt;code&gt;super&lt;/code&gt; points to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Uses of &lt;code&gt;super&lt;/code&gt; in Java
&lt;/h3&gt;

&lt;p&gt;Java allows you to use the &lt;code&gt;super&lt;/code&gt; keyword in three main ways:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Accessing Parent Class Variables
&lt;/h3&gt;

&lt;p&gt;If subclass has a variable with the same name as the parent class, use &lt;code&gt;super&lt;/code&gt; to refer to the parent’s variable.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Parent&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;x&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="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Parent&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&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;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;"Child x: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;"Parent x: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;x&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;h3&gt;
  
  
  2. Calling Parent Class Methods
&lt;/h3&gt;

&lt;p&gt;If method is overridden in the subclass, you can still access the parent class version using &lt;code&gt;super&lt;/code&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;class&lt;/span&gt; &lt;span class="nc"&gt;Parent&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;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 Parent"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Parent&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;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 Child"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;show&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Call parent method&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;h3&gt;
  
  
  3. Invoking Parent Class Constructor
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;super()&lt;/code&gt; to call the &lt;strong&gt;constructor&lt;/strong&gt; of the parent class. It must be the &lt;strong&gt;first statement&lt;/strong&gt; in the child class constructor.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Parent&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;"Parent Constructor"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Child&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Parent&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Child&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Must be first&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;"Child Constructor"&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;



</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Mastering final Keyword in Java: Variables, Methods &amp; Classes Explained</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sat, 05 Jul 2025 14:50:45 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-final-keyword-in-java-variables-methods-classes-explained-29a5</link>
      <guid>https://dev.to/mukeshb/mastering-final-keyword-in-java-variables-methods-classes-explained-29a5</guid>
      <description>&lt;h2&gt;
  
  
  Understanding the &lt;code&gt;final&lt;/code&gt; Keyword in Java
&lt;/h2&gt;

&lt;p&gt;In Java, the &lt;code&gt;final&lt;/code&gt; keyword is powerful tool used to &lt;strong&gt;restrict modification&lt;/strong&gt;. Whether applied to variables, methods, or classes, &lt;code&gt;final&lt;/code&gt; enforces immutability and prevents unexpected behavior, making your code more robust and secure.&lt;/p&gt;

&lt;p&gt;In this blog, will break down how &lt;code&gt;final&lt;/code&gt; keyword works in Java covering final variables, final methods, and final classes along with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Does &lt;code&gt;final&lt;/code&gt; Do?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;final&lt;/code&gt; keyword can be used in three main contexts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Final &lt;strong&gt;variables&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Final &lt;strong&gt;methods&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Final &lt;strong&gt;classes&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Final Variables
&lt;/h2&gt;

&lt;p&gt;When variable is declared as &lt;code&gt;final&lt;/code&gt;, &lt;strong&gt;its value cannot be changed once initialized&lt;/strong&gt;. Trying to reassign &lt;code&gt;final&lt;/code&gt; variable will result in &lt;strong&gt;compile-time error&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Compile Time Error: cannot assign value to final variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Blank Final Variable
&lt;/h3&gt;

&lt;p&gt;You can declare a &lt;code&gt;final&lt;/code&gt; variable &lt;strong&gt;without assigning value&lt;/strong&gt; this is called a &lt;strong&gt;blank final variable&lt;/strong&gt;. It must be initialized either in the &lt;strong&gt;constructor&lt;/strong&gt; or during object creation.&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;class&lt;/span&gt; &lt;span class="nc"&gt;A&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;speed&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialized in constructor&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;h3&gt;
  
  
  Static Blank Final Variable
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;static final&lt;/code&gt; variable that isn’t initialized during declaration is known as &lt;strong&gt;static blank final variable&lt;/strong&gt;. It must be initialized in &lt;strong&gt;static block&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;A&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&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;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// static blank final variable&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&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;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;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="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&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;
  
  
  2. Final Methods
&lt;/h2&gt;

&lt;p&gt;When a method is declared as &lt;code&gt;final&lt;/code&gt;, it &lt;strong&gt;cannot be overridden&lt;/strong&gt; by any subclass. This is especially useful when you want to &lt;strong&gt;prevent modification&lt;/strong&gt; of critical functionality.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Bike&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;void&lt;/span&gt; &lt;span class="nf"&gt;run&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;"Running..."&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;class&lt;/span&gt; &lt;span class="nc"&gt;Honda&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bike&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// void run() { System.out.println("Running safely"); } // Compile Time Error&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Final Classes
&lt;/h2&gt;

&lt;p&gt;If class is declared as &lt;code&gt;final&lt;/code&gt;, it &lt;strong&gt;cannot be extended&lt;/strong&gt;. This is often done for &lt;strong&gt;security&lt;/strong&gt; or to prevent alteration of core behavior.&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;Vehicle&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;start&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;"Vehicle started"&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 Car extends Vehicle {} // Compile Time Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;final&lt;/code&gt; keyword helps enforce &lt;strong&gt;immutability&lt;/strong&gt;, &lt;strong&gt;security&lt;/strong&gt;, and &lt;strong&gt;clarity&lt;/strong&gt; in Java code. Whether you are preventing variables from being modified, methods from being overridden, or classes from being extended, &lt;code&gt;final&lt;/code&gt; plays a crucial role in writing clean, error-free code.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Mastering this Keyword in Java: 5 Essential Uses Explained</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Thu, 03 Jul 2025 19:10:57 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-the-this-keyword-in-java-5-essential-uses-explained-49j0</link>
      <guid>https://dev.to/mukeshb/mastering-the-this-keyword-in-java-5-essential-uses-explained-49j0</guid>
      <description>&lt;h2&gt;
  
  
  Understanding &lt;code&gt;this&lt;/code&gt; Keyword in Java
&lt;/h2&gt;

&lt;p&gt;Java provides a powerful reference variable called &lt;code&gt;this&lt;/code&gt;, which plays crucial role in object-oriented programming. It refers to current object — the instance on which a method or constructor is invoked. In this blog post, we will explore six common usages of &lt;code&gt;this&lt;/code&gt; keyword each illustrated with examples&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Refer Current Class Instance Variable
&lt;/h2&gt;

&lt;p&gt;When local variables (method parameters or other variables) share the same name as instance variables, &lt;code&gt;this&lt;/code&gt; helps to differentiate between them.&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;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&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="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;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;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Without 'this', the parameters shadow the fields&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="c1"&gt;// Refers to the instance variable&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;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="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;printInfo&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;"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;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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&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;Here &lt;code&gt;this.name&lt;/code&gt; and &lt;code&gt;this.age&lt;/code&gt; refer to the objects fields, while &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; on right-hand side refer to constructor parameters.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Invoke Current Class Method (Implicitly)
&lt;/h2&gt;

&lt;p&gt;You can use &lt;code&gt;this&lt;/code&gt; to call another method of the same class. Although implicit calls normally work without &lt;code&gt;this&lt;/code&gt;, using it can improve code readability or resolve naming conflicts.&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;Calculator&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;square&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;x&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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&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;sumOfSquares&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;a&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;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Explicitly using 'this' to call square()&lt;/span&gt;
        &lt;span class="k"&gt;return&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;square&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&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;square&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&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 example above, &lt;code&gt;this.square()&lt;/code&gt; clearly indicates that &lt;code&gt;square&lt;/code&gt; is a method on the current object.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Invoke Current Class Constructor (&lt;code&gt;this()&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;You can chain constructors within the same class using &lt;code&gt;this()&lt;/code&gt;. It can call the default constructor or a parameterized one, simplifying object initialization.&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;Box&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Default constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Box&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Calls the parameterized constructor&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Parameterized constructor&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Box&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;length&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;width&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;height&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;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;length&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;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&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;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;height&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;Notice that &lt;code&gt;this(1, 1, 1)&lt;/code&gt; must be the first statement in default constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Pass &lt;code&gt;this&lt;/code&gt; as an Argument in Method Call
&lt;/h2&gt;

&lt;p&gt;Sometimes we need to pass current object to another method or class. We can achieve this by passing &lt;code&gt;this&lt;/code&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;Event&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;registerListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Listener&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;onEvent&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="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;interface&lt;/span&gt; &lt;span class="nc"&gt;Listener&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;onEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Event&lt;/span&gt; &lt;span class="n"&gt;e&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;Here, &lt;code&gt;this&lt;/code&gt; refers to the &lt;code&gt;Event&lt;/code&gt; instance being registered.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Pass &lt;code&gt;this&lt;/code&gt; as an Argument in Constructor Call
&lt;/h2&gt;

&lt;p&gt;Similarly, you can pass the current object to another object's constructor.&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;Engine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&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;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;car&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;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;engine&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;Engine&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="c1"&gt;// Passes the current Car instance&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 this snippet, &lt;code&gt;Car&lt;/code&gt; instance is passed to &lt;code&gt;Engine&lt;/code&gt; constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword in Java provides clarity and control when working with object instances. It helps in differentiating between variables and chaining constructors. Using &lt;code&gt;this&lt;/code&gt; enhances code readability and robustness.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Mastering the static Keyword in Java: The Ultimate Guide</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Wed, 02 Jul 2025 15:52:35 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-the-static-keyword-in-java-the-ultimate-guide-33oc</link>
      <guid>https://dev.to/mukeshb/mastering-the-static-keyword-in-java-the-ultimate-guide-33oc</guid>
      <description>&lt;h2&gt;
  
  
  Understanding the &lt;code&gt;static&lt;/code&gt; Keyword in Java
&lt;/h2&gt;

&lt;p&gt;In Java, &lt;code&gt;static&lt;/code&gt; keyword is powerful feature used primarily for memory management. It is one of those small keywords with big implications—allowing us to create members that belong to class rather than instances of class.&lt;/p&gt;

&lt;p&gt;This post explores how &lt;code&gt;static&lt;/code&gt; works with &lt;strong&gt;variables, methods, blocks&lt;/strong&gt;, and &lt;strong&gt;nested classes&lt;/strong&gt;, along with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;static&lt;/code&gt; Keyword?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;static&lt;/code&gt; keyword indicates that a &lt;strong&gt;particular member belongs to the class itself&lt;/strong&gt; and not to instances of the class. That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static variables and methods are &lt;strong&gt;shared&lt;/strong&gt; among all instances.&lt;/li&gt;
&lt;li&gt;Static blocks execute &lt;strong&gt;once&lt;/strong&gt;, at the time of class loading.&lt;/li&gt;
&lt;li&gt;Static members can be accessed &lt;strong&gt;without creating object&lt;/strong&gt; of class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This makes &lt;code&gt;static&lt;/code&gt; especially useful for scenarios where you want to store or manipulate &lt;strong&gt;common/shared data&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Static Variable (Class Variable)
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;static variable&lt;/strong&gt; is also known as a &lt;strong&gt;class variable&lt;/strong&gt;. Unlike instance variables (which get memory each time an object is created), a static variable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gets memory &lt;strong&gt;only once&lt;/strong&gt; at the time of &lt;strong&gt;class loading&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Is shared by &lt;strong&gt;all objects&lt;/strong&gt; of the class.&lt;/li&gt;
&lt;li&gt;Saves memory and promotes consistency in shared data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Use Case:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;collegeName&lt;/code&gt; for all students&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;companyName&lt;/code&gt; for all employees&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Benefits:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Saves memory&lt;/li&gt;
&lt;li&gt;Easy to maintain shared values
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;company&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ABC"&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="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="nc"&gt;Employee&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="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="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="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="o"&gt;}&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="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&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;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;company&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;
  
  
  2. Static Method
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;static method&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Belongs to the class, not to an object.&lt;/li&gt;
&lt;li&gt;Can be called &lt;strong&gt;without creating an instance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Can only access &lt;strong&gt;static data members&lt;/strong&gt; and &lt;strong&gt;static methods&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Utility/helper methods like &lt;code&gt;Math.max()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Factory methods
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Utility&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;cube&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;x&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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&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;
  
  
  3. Static Block
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;static block&lt;/strong&gt; is used to initialize static variables. It's executed &lt;strong&gt;once&lt;/strong&gt;, when the class is loaded &lt;strong&gt;before the &lt;code&gt;main()&lt;/code&gt; method&lt;/strong&gt; runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Demo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&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;"Static block executed."&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;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="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;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;"Main method executed."&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;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Static block executed.
Main method executed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;static&lt;/code&gt; keyword allows memory sharing and promotes consistent behavior across all objects, helping reduce redundancy and boost performance. It's especially useful when building utility classes, caching shared data, or initializing configurations during class loading.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Advanced JavaScript: Exploring the Event Loop</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sat, 20 Jul 2024 08:41:43 +0000</pubDate>
      <link>https://dev.to/mukeshb/advanced-javascript-exploring-the-event-loop-1063</link>
      <guid>https://dev.to/mukeshb/advanced-javascript-exploring-the-event-loop-1063</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;JavaScript is a powerful and widely-used programming language in web development. One of its most interesting aspect is its concurrency model, which allows it to handle multiple tasks efficiently despite being single-threaded. Understanding the event loop is crucial for writing performant and bug-free JavaScript code, especially in complex applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding JavaScript's Concurrency Model
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Single-Threaded Nature
&lt;/h3&gt;

&lt;p&gt;JavaScript is single-threaded, meaning it executes code sequentially, one operation at a time. This is in contrast to languages that are multi-threaded, where multiple threads can run concurrently. However, JavaScript uses an event-driven, non-blocking architecture to manage concurrency and handle asynchronous tasks efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency Model
&lt;/h3&gt;

&lt;p&gt;JavaScript uses a concurrency model based on an &lt;strong&gt;event loop&lt;/strong&gt;, which allows it to perform non-blocking operations. This model is essential for handling tasks like I/O operations, network requests, and user interactions without freezing the user interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Event Loop Explained
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the Event Loop?
&lt;/h3&gt;

&lt;p&gt;The event loop is the mechanism that JavaScript uses to coordinate the execution of code, handle events, and manage asynchronous tasks. It continuously checks the call stack to see if there’s any function that needs to run, and processes tasks in the callback queue when the stack is empty.&lt;/p&gt;

&lt;h3&gt;
  
  
  Components of the Event Loop
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. Call Stack
&lt;/h3&gt;

&lt;p&gt;The call stack keeps track of function calls. When a function is invoked, it's added to the stack, and when it completes, it's removed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayGoodbye&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Goodbye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;sayGoodbye&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;greet()&lt;/code&gt; is called and added to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("Hello")&lt;/code&gt; is executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;greet()&lt;/code&gt; is removed from the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sayGoodbye()&lt;/code&gt; is called and added to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("Goodbye")&lt;/code&gt; is executed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sayGoodbye()&lt;/code&gt; is removed from the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Web APIs
&lt;/h3&gt;

&lt;p&gt;Web APIs are provided by the browser (or Node.js) and include features like &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;DOM events&lt;/code&gt;, &lt;code&gt;fetch&lt;/code&gt;, etc. They are used to perform tasks that are outside the main execution thread.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log("Start")&lt;/code&gt; is executed and logged immediately.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; is called and its callback is sent to the web API environment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("End")&lt;/code&gt; is executed and logged immediately.&lt;/li&gt;
&lt;li&gt;After 1 second, the callback from &lt;code&gt;setTimeout&lt;/code&gt; is pushed to the callback queue.&lt;/li&gt;
&lt;li&gt;Once the call stack is empty, the event loop pushes the callback to the stack, and &lt;code&gt;console.log("Timeout")&lt;/code&gt; is executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Callback Queue (Task Queue)
&lt;/h3&gt;

&lt;p&gt;The callback queue holds messages with callbacks to be processed. The event loop takes tasks from the queue and adds them to the call stack for execution when the stack is empty.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Microtask Queue
&lt;/h3&gt;

&lt;p&gt;The microtask queue is used for tasks that need to run immediately after the current operation completes. Promises and mutation observers are handled here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Timeout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;End&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log("Start")&lt;/code&gt; is executed and logged.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; callback is sent to the callback queue with zero delay.&lt;/li&gt;
&lt;li&gt;A promise is resolved, and its callback is added to the microtask queue.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("End")&lt;/code&gt; is executed and logged.&lt;/li&gt;
&lt;li&gt;The event loop processes the microtask queue and logs &lt;code&gt;console.log("Promise")&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The callback queue is processed, logging &lt;code&gt;console.log("Timeout")&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript ES6+ Features: A Complete Overview</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sat, 13 Jul 2024 11:25:17 +0000</pubDate>
      <link>https://dev.to/mukeshb/javascript-es6-features-a-complete-overview-15a7</link>
      <guid>https://dev.to/mukeshb/javascript-es6-features-a-complete-overview-15a7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;JavaScript is constantly evolving, and keeping up with the latest features is crucial for modern web development. Since ES6, JavaScript has introduced new features that make coding more efficient.&lt;/p&gt;

&lt;p&gt;Understanding and utilizing the latest JavaScript features can greatly enhance your productivity and code quality, making your applications more robust and maintainable.&lt;/p&gt;

&lt;p&gt;In this blog, will provide overview of the key features introduced in ES6 and beyond, including let and const, arrow functions and more.&lt;/p&gt;

&lt;p&gt;Key Features Introduced in ES6+&lt;/p&gt;

&lt;h2&gt;
  
  
  1. let and const
&lt;/h2&gt;

&lt;p&gt;JavaScript introduced two new ways to declare variables let and const. These provide better scoping and more control over how variables are used in your code compared to the traditional var.&lt;/p&gt;

&lt;h3&gt;
  
  
  let
&lt;/h3&gt;

&lt;p&gt;let is used to declare variables that can be reassigned. Unlike var, let has block scope, meaning it is only accessible within the block (curly braces {}) where it is defined.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: x is not defined&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;if&lt;/code&gt; statement checks if the condition &lt;code&gt;true&lt;/code&gt; is met (which it always is in this case).&lt;/li&gt;
&lt;li&gt;When the condition is true, the code block &lt;code&gt;{ ... }&lt;/code&gt; executes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let x = 10;&lt;/code&gt; declares a variable &lt;code&gt;x&lt;/code&gt; with block scope (scoped to the nearest curly braces &lt;code&gt;{ ... }&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is assigned the value &lt;code&gt;10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(x);&lt;/code&gt; outputs the value of &lt;code&gt;x&lt;/code&gt;, which is &lt;code&gt;10&lt;/code&gt;, to the console.&lt;/li&gt;
&lt;li&gt;The output displayed is &lt;code&gt;10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(x);&lt;/code&gt; attempts to output the value of &lt;code&gt;x&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;However, &lt;code&gt;x&lt;/code&gt; is not accessible outside the block where it was defined (&lt;code&gt;if&lt;/code&gt; block).&lt;/li&gt;
&lt;li&gt;JavaScript throws an error: &lt;code&gt;ReferenceError: x is not defined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This error occurs because &lt;code&gt;let&lt;/code&gt; variables are block-scoped and exist only within the block (curly braces) where they are defined.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  const
&lt;/h3&gt;

&lt;p&gt;const is used to declare variables that should not be reassigned. Like let, const has block scope. Once a variable is assigned a value using const, it cannot be changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 20&lt;/span&gt;
&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: Assignment to constant variable.&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; declares a constant variable named &lt;code&gt;y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Constants (&lt;code&gt;const&lt;/code&gt;) are variables that cannot be reassigned once their value is set.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;y&lt;/code&gt; is initialized with the value &lt;code&gt;20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(y);&lt;/code&gt; outputs the value of &lt;code&gt;y&lt;/code&gt;, which is &lt;code&gt;20&lt;/code&gt;, to the console.&lt;/li&gt;
&lt;li&gt;The output displayed is &lt;code&gt;20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attempts to reassign the value of &lt;code&gt;y&lt;/code&gt; to &lt;code&gt;30&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;However, constants (&lt;code&gt;const&lt;/code&gt;) in JavaScript cannot be reassigned after their initial assignment.&lt;/li&gt;
&lt;li&gt;JavaScript throws an error: &lt;code&gt;TypeError: Assignment to constant variable.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This error occurs because &lt;code&gt;y&lt;/code&gt; is declared as a constant (&lt;code&gt;const&lt;/code&gt;), and you cannot change its value once it's set.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Arrow Functions
&lt;/h2&gt;

&lt;p&gt;Description:&lt;br&gt;
Arrow functions provide a concise syntax for writing function expressions. They do not have their own this context, making them particularly useful for callbacks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Traditional function expression&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Arrow function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var add&lt;/code&gt; declares a variable named &lt;code&gt;add&lt;/code&gt;. This variable will store a function.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;function(a, b)&lt;/code&gt; part creates an anonymous function (a function without a name) that takes two parameters, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside the curly braces {}, the function body contains a single statement: &lt;code&gt;return a + b;&lt;/code&gt;. This means the function will add the two parameters &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and return the result.&lt;/li&gt;
&lt;li&gt;The function is assigned to the variable &lt;code&gt;add&lt;/code&gt;. Now, &lt;code&gt;add&lt;/code&gt; can be used to call the function and perform the addition.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;add(2, 3)&lt;/code&gt; is called, the function adds &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; and returns &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const add&lt;/code&gt; declares a constant variable named &lt;code&gt;add&lt;/code&gt;. This means the variable &lt;code&gt;add&lt;/code&gt; cannot be reassigned to a different value or function.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;(a, b) =&amp;gt; a + b&lt;/code&gt; part is an arrow function. It is a shorter and more concise way to write a function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(a, b)&lt;/code&gt; lists the parameters of the function, just like in the traditional function expression.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;a + b&lt;/code&gt; part is the body of the function. Since there are no curly braces &lt;code&gt;{}&lt;/code&gt;, this function has an implicit return, meaning it automatically returns the result of &lt;code&gt;a + b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;add(2, 3)&lt;/code&gt; is called, the arrow function adds &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; and returns &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Template Literals
&lt;/h2&gt;

&lt;p&gt;Template literals let us create strings that can span multiple lines and include variables easily. Use template literals for embedding variables and expressions in strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello, John!&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; declares a constant variable named &lt;code&gt;name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'John'&lt;/code&gt; is assigned as the value of &lt;code&gt;name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Constants &lt;code&gt;const&lt;/code&gt; are variables that cannot be reassigned once their value is set.&lt;/li&gt;
&lt;li&gt;Template literals (enclosed in backticks) allow embedding expressions inside strings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;${name}&lt;/code&gt; within &lt;code&gt;${}&lt;/code&gt; is an expression that gets replaced with the value of the &lt;code&gt;name&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;${name}&lt;/code&gt; evaluates to &lt;code&gt;'John'&lt;/code&gt;, so &lt;code&gt;greeting&lt;/code&gt; becomes &lt;code&gt;'Hello, John!'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log()&lt;/code&gt; outputs the value of &lt;code&gt;greeting&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;The output displayed in the console is &lt;code&gt;Hello, John!&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Destructuring Assignment
&lt;/h2&gt;

&lt;p&gt;Destructuring allows you to unpack values from arrays or properties from objects into distinct variables. Use destructuring to simplify the extraction of values from arrays and objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// John&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; declares a constant variable named &lt;code&gt;user&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user&lt;/code&gt; is an object with two properties: &lt;code&gt;name&lt;/code&gt; with value &lt;code&gt;'John'&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; with value &lt;code&gt;30&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Object destructuring allows you to extract specific properties from an object into variables with the same names.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{ name, age }&lt;/code&gt; on the left-hand side of &lt;code&gt;=&lt;/code&gt; declares two new variables (&lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;These variables are assigned the values of &lt;code&gt;user.name&lt;/code&gt; and &lt;code&gt;user.age&lt;/code&gt;, respectively.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(name);&lt;/code&gt; outputs the value of the variable &lt;code&gt;name&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;The output displayed in the console is &lt;code&gt;John&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(age);&lt;/code&gt; outputs the value of the variable &lt;code&gt;age&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;The output displayed in the console is &lt;code&gt;30&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Default Parameters
&lt;/h2&gt;

&lt;p&gt;Default parameters allow you to set default values for function parameters if no value or undefined is passed. Use default parameters to ensure functions have default values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Guest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Guest!&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;function greet(...)&lt;/code&gt; declares a function named &lt;code&gt;greet&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name = 'Guest'&lt;/code&gt; is a parameter with a default value of &lt;code&gt;'Guest'&lt;/code&gt;. This means if no argument is passed to &lt;code&gt;greet()&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt; will default to &lt;code&gt;'Guest'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Template literals (enclosed in backticks &lt;code&gt;`&lt;/code&gt;) allow embedding expressions inside strings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;${name}&lt;/code&gt; within &lt;code&gt;${}&lt;/code&gt; is an expression that gets replaced with the value of the &lt;code&gt;name&lt;/code&gt; parameter passed to the function.&lt;/li&gt;
&lt;li&gt;If no &lt;code&gt;name&lt;/code&gt; parameter is provided, it defaults to &lt;code&gt;'Guest'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;greet()&lt;/code&gt; calls the &lt;code&gt;greet&lt;/code&gt; function without passing any arguments.&lt;/li&gt;
&lt;li&gt;Since no argument is passed, &lt;code&gt;name&lt;/code&gt; defaults to &lt;code&gt;'Guest'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function then logs &lt;code&gt;'Hello, Guest!'&lt;/code&gt; to the console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Rest and Spread Operators
&lt;/h2&gt;

&lt;p&gt;The rest operator (...) allows you to represent an indefinite number of arguments as an array. The spread operator (...) allows an iterable to be expanded in places where zero or more arguments or elements are expected. Use rest operator in function parameters to handle various arguments. Use spread operator to expand arrays and objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4]&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;function sum(...numbers)&lt;/code&gt; declares a function named &lt;code&gt;sum&lt;/code&gt; that uses a rest parameter &lt;code&gt;...numbers&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The rest parameter &lt;code&gt;...numbers&lt;/code&gt; allows the function to accept any number of arguments and gathers them into an array named &lt;code&gt;numbers&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;numbers.reduce(...)&lt;/code&gt; applies the &lt;code&gt;reduce&lt;/code&gt; method on the &lt;code&gt;numbers&lt;/code&gt; array.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;reduce&lt;/code&gt; method iterates over each element in the &lt;code&gt;numbers&lt;/code&gt; array and accumulates a single value (&lt;code&gt;a + b&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(a, b) =&amp;gt; a + b&lt;/code&gt; is an arrow function that adds two numbers &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; is the initial value of &lt;code&gt;a&lt;/code&gt; in the reduction process.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sum(1, 2, 3)&lt;/code&gt; calls the &lt;code&gt;sum&lt;/code&gt; function with three arguments: &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, and &lt;code&gt;3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function calculates the sum of these numbers (&lt;code&gt;1 + 2 + 3&lt;/code&gt;) using &lt;code&gt;reduce&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The result &lt;code&gt;6&lt;/code&gt; is logged to the console.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const arr1&lt;/code&gt; declares a constant variable &lt;code&gt;arr1&lt;/code&gt; initialized with an array &lt;code&gt;[1, 2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;...arr1&lt;/code&gt; spreads the elements of &lt;code&gt;arr1&lt;/code&gt; into the new array &lt;code&gt;arr2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[...arr1, 3, 4]&lt;/code&gt; creates a new array &lt;code&gt;arr2&lt;/code&gt; by combining the elements of &lt;code&gt;arr1&lt;/code&gt; (&lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;) with &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log(arr2)&lt;/code&gt; outputs the contents of &lt;code&gt;arr2&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;The output displayed is &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;, which is the combined array with elements from &lt;code&gt;arr1&lt;/code&gt; and additional elements &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Classes
&lt;/h2&gt;

&lt;p&gt;Classes provide a more intuitive and simpler syntax for creating objects and dealing with inheritance in JavaScript. Use classes to create objects and establish inheritance hierarchies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;john&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="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hello, my name is John and I am 30 years old.&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;class Person&lt;/code&gt; declares a new class named &lt;code&gt;Person&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Classes in JavaScript provide a way to define blueprints for creating objects with shared methods and properties.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;constructor&lt;/code&gt; method is a special method for creating and initializing objects created with a class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;constructor(name, age)&lt;/code&gt; defines parameters &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this.name = name;&lt;/code&gt; assigns the value of &lt;code&gt;name&lt;/code&gt; passed to the constructor to the &lt;code&gt;name&lt;/code&gt; property of the object being created (&lt;code&gt;this&lt;/code&gt; refers to the current instance of the &lt;code&gt;Person&lt;/code&gt; class).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this.age = age;&lt;/code&gt; assigns the value of &lt;code&gt;age&lt;/code&gt; passed to the constructor to the &lt;code&gt;age&lt;/code&gt; property of the object being created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;greet()&lt;/code&gt; defines a method &lt;code&gt;greet&lt;/code&gt; within the &lt;code&gt;Person&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;Methods in classes are functions that can be called on instances of the class.&lt;/li&gt;
&lt;li&gt;Template literals (enclosed in backticks &lt;code&gt;`&lt;/code&gt;) allow embedding expressions inside strings.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;${this.name}&lt;/code&gt; and &lt;code&gt;${this.age}&lt;/code&gt; are expressions that are replaced with the values of &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties of the current object (&lt;code&gt;this&lt;/code&gt; refers to the current instance of &lt;code&gt;Person&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Person('John', 30)&lt;/code&gt; creates a new instance of the &lt;code&gt;Person&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;constructor&lt;/code&gt; method is automatically called with arguments &lt;code&gt;'John'&lt;/code&gt; and &lt;code&gt;30&lt;/code&gt;, initializing the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties of the &lt;code&gt;john&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;john.greet()&lt;/code&gt; calls the &lt;code&gt;greet&lt;/code&gt; method on the &lt;code&gt;john&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;The method logs &lt;code&gt;"Hello, my name is John and I am 30 years old."&lt;/code&gt; to the console, using the values stored in &lt;code&gt;john.name&lt;/code&gt; and &lt;code&gt;john.age&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Promises
&lt;/h2&gt;

&lt;p&gt;Promises provide a way to handle asynchronous operations more gracefully than callbacks. Use promises for handling asynchronous operations such as API calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Data fetched&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Data fetched&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const fetchData&lt;/code&gt; declares a constant variable &lt;code&gt;fetchData&lt;/code&gt; initialized with an arrow function &lt;code&gt;() =&amp;gt; { ... }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Arrow functions provide a concise way to write functions in JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Promise(...)&lt;/code&gt; creates a new &lt;code&gt;Promise&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Promise&lt;/code&gt; constructor takes a function with &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; parameters.&lt;/li&gt;
&lt;li&gt;Inside this function:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setTimeout(..., 1000)&lt;/code&gt; schedules a function to be executed after a delay of 1000 milliseconds (1 second).&lt;/li&gt;
&lt;li&gt;The arrow function &lt;code&gt;() =&amp;gt; { resolve('Data fetched'); }&lt;/code&gt; is executed after the timeout.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resolve('Data fetched')&lt;/code&gt; fulfills the promise with the value &lt;code&gt;'Data fetched'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;fetchData()&lt;/code&gt; calls the &lt;code&gt;fetchData&lt;/code&gt; function, which returns a &lt;code&gt;Promise&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;.then(data =&amp;gt; { ... })&lt;/code&gt; attaches a callback function to handle the resolved value (&lt;code&gt;data&lt;/code&gt;) when the promise is fulfilled.&lt;/li&gt;

&lt;li&gt;The arrow function &lt;code&gt;data =&amp;gt; { console.log(data); }&lt;/code&gt; is executed once the promise is resolved successfully (&lt;code&gt;resolve('Data fetched')&lt;/code&gt;).&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;console.log(data)&lt;/code&gt; outputs the value of &lt;code&gt;data&lt;/code&gt; ('Data fetched') to the console.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. Async/Await
&lt;/h2&gt;

&lt;p&gt;Async/await allows you to write asynchronous code in a more synchronous and readable manner.Use async/await for clearer and more readable asynchronous code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Data fetched&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Data fetched&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const fetchData&lt;/code&gt; declares a constant variable &lt;code&gt;fetchData&lt;/code&gt; initialized with an arrow function &lt;code&gt;() =&amp;gt; { ... }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Arrow functions provide a concise way to define functions in JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Promise(...)&lt;/code&gt; creates a new &lt;code&gt;Promise&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;The promise executor function takes two parameters: &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside the executor function:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setTimeout(..., 1000)&lt;/code&gt; schedules a function to be executed after a delay of 1000 milliseconds (1 second).&lt;/li&gt;
&lt;li&gt;The arrow function &lt;code&gt;() =&amp;gt; { resolve('Data fetched'); }&lt;/code&gt; is executed after the timeout completes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resolve('Data fetched')&lt;/code&gt; fulfills the promise with the value &lt;code&gt;'Data fetched'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;async function getData()&lt;/code&gt; declares an asynchronous function named &lt;code&gt;getData&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Async functions allow you to write asynchronous code in a synchronous-like manner, making it easier to work with promises and other asynchronous operations.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;await fetchData()&lt;/code&gt; pauses the execution of &lt;code&gt;getData&lt;/code&gt; until the promise returned by &lt;code&gt;fetchData&lt;/code&gt; settles (fulfills or rejects).&lt;/li&gt;

&lt;li&gt;Once fulfilled, &lt;code&gt;await&lt;/code&gt; returns the resolved value (&lt;code&gt;'Data fetched'&lt;/code&gt;) and assigns it to the variable &lt;code&gt;data&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;console.log(data);&lt;/code&gt; outputs the value of &lt;code&gt;data&lt;/code&gt; ('Data fetched') to the console.&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;getData()&lt;/code&gt; calls the &lt;code&gt;getData&lt;/code&gt; function, initiating the execution of asynchronous operations defined inside &lt;code&gt;getData&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;ES6+ features provide powerful tools for writing modern JavaScript. By understanding and using these features, developers can write more efficient, readable, and maintainable code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Mastering Asynchronous JavaScript: Promises, Async/Await, and Callbacks</title>
      <dc:creator>Mukesh</dc:creator>
      <pubDate>Sun, 07 Jul 2024 07:15:15 +0000</pubDate>
      <link>https://dev.to/mukeshb/mastering-asynchronous-javascript-promises-asyncawait-and-callbacks-3p7e</link>
      <guid>https://dev.to/mukeshb/mastering-asynchronous-javascript-promises-asyncawait-and-callbacks-3p7e</guid>
      <description>&lt;p&gt;Many operations in the real world, such as retrieving data from a server or reading a file, take time to finish. If these operations were synchronous, then we need to wait until the entire task is completed, resulting in a bad user experience. Asynchronous operations enable the program to continue executing other tasks while these operations are complete.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Understanding Callbacks
&lt;/h2&gt;

&lt;p&gt;Callbacks are functions passed as arguments to other functions, which are then invoked when the task is complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example of a callback function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data fetched from server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Using the callback&lt;/span&gt;
&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Data fetched!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Function Definition:
&lt;/h4&gt;

&lt;p&gt;The function &lt;code&gt;fetchData&lt;/code&gt; is defined to accept one argument, &lt;code&gt;callback&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;callback&lt;/code&gt; is expected to be a function that will be called once the asynchronous operation inside &lt;code&gt;fetchData&lt;/code&gt; is complete.&lt;/p&gt;
&lt;h4&gt;
  
  
  setTimeout:
&lt;/h4&gt;

&lt;p&gt;Inside &lt;code&gt;fetchData&lt;/code&gt;, there is a call to &lt;code&gt;setTimeout&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;setTimeout&lt;/code&gt; is a built-in JavaScript function that executes a function after a specified delay in milliseconds.&lt;br&gt;
it is used to simulate a delay of 2000 milliseconds (2 seconds) to mimic an asynchronous operation like fetching data from a server.&lt;/p&gt;
&lt;h4&gt;
  
  
  Callback Invocation:
&lt;/h4&gt;

&lt;p&gt;After the 2-second delay, the anonymous function passed to &lt;code&gt;setTimeout&lt;/code&gt; is executed.&lt;br&gt;
This function calls &lt;code&gt;callback&lt;/code&gt; with the argument &lt;code&gt;"Data fetched from server"&lt;/code&gt;.&lt;br&gt;
This means after the delay, the &lt;code&gt;callback&lt;/code&gt; function will be executed with the provided message.&lt;/p&gt;
&lt;h4&gt;
  
  
  Calling fetchData:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;fetchData&lt;/code&gt; is called with an anonymous function as its argument. This anonymous function takes one parameter, &lt;code&gt;message&lt;/code&gt;.&lt;br&gt;
The anonymous function is the callback function that will be executed once the data is "fetched" (after the 2-second delay).&lt;/p&gt;
&lt;h4&gt;
  
  
  Callback Execution:
&lt;/h4&gt;

&lt;p&gt;After 2 seconds, &lt;code&gt;setTimeout&lt;/code&gt; triggers the execution of the callback with the message &lt;code&gt;"Data fetched from server"&lt;/code&gt;.&lt;br&gt;
The callback function logs the message to the console.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Working with Promises
&lt;/h2&gt;

&lt;p&gt;A promise is an object representing the eventual completion or failure of an asynchronous call. It provides methods to handle asynchronous results and handle errors. It provide a consistent way to handle errors using .catch().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data fetched from server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Data fetched!&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Function Definition:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fetchData&lt;/code&gt; function is defined to return a new promise.&lt;br&gt;
A promise is an object that represents an asynchronous operation that will eventually complete or fail.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating the Promise:
&lt;/h3&gt;

&lt;p&gt;Inside &lt;code&gt;fetchData&lt;/code&gt;, a new promise is created using &lt;code&gt;new Promise((resolve, reject) =&amp;gt; {...})&lt;/code&gt;.&lt;br&gt;
The promise constructor takes a function with two parameters, &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;. These are functions that you call when the asynchronous operation completes successfully (&lt;code&gt;resolve&lt;/code&gt;) or fails (&lt;code&gt;reject&lt;/code&gt;).&lt;/p&gt;
&lt;h3&gt;
  
  
  Simulating an Asynchronous Operation:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;setTimeout&lt;/code&gt; function is used to simulate a delay of 2000 milliseconds (2 seconds), representing an asynchronous operation like fetching data from a server.&lt;br&gt;
After the 2-second delay, the resolve function is called with the message &lt;code&gt;"Data fetched from server"&lt;/code&gt;.&lt;br&gt;
This means the promise is successfully fulfilled after 2 seconds with the specified message.&lt;/p&gt;
&lt;h3&gt;
  
  
  Calling fetchData:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fetchData&lt;/code&gt; function is called, which returns a promise.&lt;/p&gt;
&lt;h3&gt;
  
  
  Handling the Promise:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; method is chained to the promise returned by &lt;code&gt;fetchData&lt;/code&gt;.&lt;br&gt;
The function passed to &lt;code&gt;.then()&lt;/code&gt; is called when the promise is resolved successfully. It receives the message &lt;code&gt;"Data fetched from server"&lt;/code&gt; and logs it to the console.&lt;/p&gt;
&lt;h3&gt;
  
  
  Error Handling:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;.catch()&lt;/code&gt; method is chained to handle any errors if the promise is rejected.&lt;br&gt;
If there were an error (for example, if &lt;code&gt;reject&lt;/code&gt; were called inside the promise), the function passed to &lt;code&gt;.catch()&lt;/code&gt; would be called to log the error.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Async/Await: Modern Asynchronous JavaScript
&lt;/h2&gt;

&lt;p&gt;Async and Await are powerful keywords used for handling asynchronous operations using promises. Async functions returns a promise and await pauses the execution until Promise is resolved. It simplify asynchronous code and improve readability by making it appear synchronous.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data fetched from server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Data fetched!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Explanation
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Async Function:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fetchData&lt;/code&gt; function is declared with the &lt;code&gt;async&lt;/code&gt; keyword. This means it can use &lt;code&gt;await&lt;/code&gt; inside it and it returns a promise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Try-Catch Block:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;try&lt;/code&gt; block contains code that might throw an error. If an error occurs, the control moves to the &lt;code&gt;catch&lt;/code&gt; block to handle the error.&lt;br&gt;
The &lt;code&gt;catch&lt;/code&gt; block catches and handles any errors that occur inside the try block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Awaiting a Promise:
&lt;/h3&gt;

&lt;p&gt;Inside the &lt;code&gt;try&lt;/code&gt; block, we have an &lt;code&gt;await&lt;/code&gt; expression. It waits for the promise to resolve before moving to the next line of code.&lt;br&gt;
The promise is created using &lt;code&gt;new Promise((resolve) =&amp;gt; {...})&lt;/code&gt;.&lt;br&gt;
The &lt;code&gt;setTimeout&lt;/code&gt; function is used to simulate a delay of 2000 milliseconds (2 seconds), representing an asynchronous operation like fetching data from a server.&lt;br&gt;
After 2 seconds, the promise is resolved with the message &lt;code&gt;"Data fetched from server"&lt;/code&gt;, and this message is assigned to the &lt;code&gt;message&lt;/code&gt; variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logging the Message:
&lt;/h3&gt;

&lt;p&gt;After the promise is resolved, the message is logged to the console using &lt;code&gt;console.log(message)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling:
&lt;/h3&gt;

&lt;p&gt;If an error occurs while waiting for the promise or during any operation in the &lt;code&gt;try&lt;/code&gt; block, the control moves to the &lt;code&gt;catch&lt;/code&gt; block.&lt;br&gt;
The error is logged to the console using &lt;code&gt;console.error(error)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling the Async Function:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fetchData&lt;/code&gt; function is called. Since it's an &lt;code&gt;async&lt;/code&gt; function, it runs asynchronously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison Summary
&lt;/h2&gt;

&lt;p&gt;Callbacks are simple and direct but can lead to messy code and inconsistent error handling.&lt;/p&gt;

&lt;p&gt;Promises offer a more structured way to handle asynchronous operations and provide better error handling.&lt;/p&gt;

&lt;p&gt;Async/Await builds on promises to provide even cleaner, more readable, and maintainable asynchronous code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
