<?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: Ravi Sarva</title>
    <description>The latest articles on DEV Community by Ravi Sarva (@ravi_sarva).</description>
    <link>https://dev.to/ravi_sarva</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%2F1176010%2Fc266dd1a-d9ed-4e90-9cf6-2f80011c91bd.JPG</url>
      <title>DEV Community: Ravi Sarva</title>
      <link>https://dev.to/ravi_sarva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ravi_sarva"/>
    <language>en</language>
    <item>
      <title>🚀 Understanding Method Overriding, Method Hiding, and Overloading in Java 🚀</title>
      <dc:creator>Ravi Sarva</dc:creator>
      <pubDate>Mon, 01 Apr 2024 16:11:49 +0000</pubDate>
      <link>https://dev.to/ravi_sarva/understanding-method-overriding-method-hiding-and-overloading-in-java-53o4</link>
      <guid>https://dev.to/ravi_sarva/understanding-method-overriding-method-hiding-and-overloading-in-java-53o4</guid>
      <description>&lt;p&gt;In Java, developers often encounter concepts like &lt;strong&gt;Method Overriding&lt;/strong&gt;, &lt;strong&gt;Method Hiding&lt;/strong&gt;, and &lt;strong&gt;Method Overloading&lt;/strong&gt;. These concepts empower polymorphism, enhancing code clarity and flexibility. Let's explore each concept using examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Method Overriding:
&lt;/h2&gt;

&lt;p&gt;Method overriding occurs when a subclass provides its specific implementation of a method inherited from its superclass. This enables altering method behavior while maintaining the method name and signature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class P {
    public void m1() {
        System.out.println("P class m1 method is called");
    }

    class C extends P {
        @Override
        public void m1() {
            System.out.println("C class m1 method is called");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        P p = new P();
        p.m1();

        P.C c = new P().new C();
        c.m1();

        P p1 = new P().new C();
        p1.m1();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The output will be :&lt;br&gt;
P class m1 method is called&lt;br&gt;
C class m1 method is called&lt;br&gt;
C class m1 method is called&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Method Hiding:
&lt;/h2&gt;

&lt;p&gt;Method hiding, on the other hand, occurs when a subclass defines a static method with the same signature as a static method in its superclass. Unlike method overriding, where the subclass method overrides the superclass method's behavior, in method hiding, the subclass method shadows the superclass method.&lt;/p&gt;

&lt;p&gt;Let's consider an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class P {
    public static void m1() {
        System.out.println("P class m1 method is called");
    }
    class C extends P {
        public static void m1() {
            System.out.println("C class m1 method is called");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, C class defines a static method m1() with the same signature as the static method m1() in class P. However, this is not method overriding; it's method hiding. When invoking static methods, Java binds the method call at compile time, unlike dynamic dispatch in method overriding.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The output will be :&lt;br&gt;
P class m1 method is called&lt;br&gt;
P class m1 method is called&lt;br&gt;
P class m1 method is called&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Understanding method overriding, method hiding, and method overloading is essential for writing clean, flexible, and maintainable Java code. Method overriding enables subclass-specific behavior, method hiding allows subclass methods to shadow superclass methods, and method overloading provides flexibility in method invocation with different parameter lists. By mastering these concepts, Java developers can leverage the power of polymorphism and write efficient and expressive code.&lt;/p&gt;

</description>
      <category>java</category>
      <category>softwaredevelopment</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
    <item>
      <title>🚀 Mastering Array Rotation in Java: Algorithms and Time Complexities 🕰️</title>
      <dc:creator>Ravi Sarva</dc:creator>
      <pubDate>Tue, 03 Oct 2023 14:53:50 +0000</pubDate>
      <link>https://dev.to/ravi_sarva/mastering-array-rotation-in-java-algorithms-and-time-complexities-3ca2</link>
      <guid>https://dev.to/ravi_sarva/mastering-array-rotation-in-java-algorithms-and-time-complexities-3ca2</guid>
      <description>&lt;p&gt;Are you ready to take your Java programming skills to the next level? Let's dive into the fascinating world of array manipulation as we tackle the "Rotate an Array" problem with finesse! 🌟&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fENx2OsS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgdftkjjovm20ktoufn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fENx2OsS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgdftkjjovm20ktoufn4.png" alt="Image description" width="646" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;br&gt;
Imagine you have an array of integers, and you want to perform a cyclic right rotation on it by a certain number of positions, say k. Sounds tricky? Don't worry; we've got some nifty Java solutions for you!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
Given the input array [1, 2, 3, 4, 5, 6, 7] and k = 3, the expected output is [5, 6, 7, 1, 2, 3, 4]. Magic, right? 🪄&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌟 Solution 1: The Brute Force Approach 🌟&lt;/strong&gt;&lt;br&gt;
Time Complexity: O(n * k)&lt;br&gt;
In this approach, we roll up our sleeves and rotate the array one step at a time for k steps. It might not be the most efficient solution, but it's a great way to start understanding the problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Java code for rotating an array using brute force
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n; // handle cases where k &amp;gt; n
        for (int i = 0; i &amp;lt; k; i++) {
            int temp = nums[n - 1];
            for (int j = n - 1; j &amp;gt; 0; j--) {
                nums[j] = nums[j - 1];
            }
            nums[0] = temp;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🚀 Solution 2: Using Extra Space 🚀&lt;/strong&gt;&lt;br&gt;
Time Complexity: O(n)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Java code for rotating an array using extra space
public void rotate(int[] nums, int k) {
    int n = nums.length;
    int[] result = new int[n];
    for (int i = 0; i &amp;lt; n; i++) {
        result[(i + k) % n] = nums[i];
    }
    System.arraycopy(result, 0, nums, 0, n);
}

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

&lt;/div&gt;



&lt;p&gt;Next up, we take a more space-efficient approach. We create a new array and elegantly copy the rotated elements to it. This solution offers a significant improvement in time complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌪️ Solution 3: Reversal Algorithm 🌪️&lt;/strong&gt;&lt;br&gt;
Time Complexity: O(n)&lt;/p&gt;

&lt;p&gt;In this solution, we take a smart approach. We reverse the array in parts: first, the entire array, then the first k elements, and finally the remaining elements. This results in an efficient solution with an optimal time complexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Java code for rotating an array using the reversal algorithm
public void rotate(int[] nums, int k) {
    int n = nums.length;
    k = k % n;
    reverse(nums, 0, n - 1);
    reverse(nums, 0, k - 1);
    reverse(nums, k, n - 1);
}

private void reverse(int[] nums, int start, int end) {
    while (start &amp;lt; end) {
        int temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start++;
        end--;
    }
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🎉 Conclusion 🎉&lt;/strong&gt;&lt;br&gt;
You've just unlocked three powerful techniques to tackle the "Rotate an Array" problem in Java! Each solution comes with its own time complexity, allowing you to choose the best fit for your specific problem and performance requirements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Brute Force Approach (Time Complexity: O(n * k)): Simple and intuitive.&lt;/li&gt;
&lt;li&gt;Using Extra Space (Time Complexity: O(n)): Space-efficient and effective.&lt;/li&gt;
&lt;li&gt;Reversal Algorithm (Time Complexity: O(n)): Smart and optimal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to ask questions, share your thoughts, or dive deeper into these solutions in the comments section below! 🤓&lt;/p&gt;

&lt;p&gt;Happy coding, and remember, the sky's the limit when you master your algorithms! ✨&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
