𝐓𝐡𝐞 𝐉𝐚𝐯𝐚 𝐟𝐞𝐚𝐭𝐮𝐫𝐞 𝐧𝐨𝐛𝐨𝐝𝐲 𝐭𝐚𝐥𝐤𝐬 𝐚𝐛𝐨𝐮𝐭 — 𝐛𝐮𝐭 𝐞𝐯𝐞𝐫𝐲 𝐟𝐥𝐮𝐞𝐧𝐭 𝐀𝐏𝐈 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐝𝐞𝐩𝐞𝐧𝐝𝐬 𝐨𝐧. A few nights ago, while refactoring some old Java code, I noticed something that made me pause. A subclass method had the same name and parameters as its parent… but it returned a different type. And to my surprise — it compiled perfectly. That’s when I re-discovered one of Java’s most underrated features: 👉 Covariant Return Types In simple terms: When a subclass overrides a method, it can return a more specific type than the parent Before Java 5, this wasn’t possible. Now it’s what quietly powers builder patterns, method chaining, and fluent APIs. Because the compiler says: “𝐈𝐟 𝐲𝐨𝐮𝐫 𝐬𝐮𝐛𝐜𝐥𝐚𝐬𝐬 𝐩𝐫𝐨𝐦𝐢𝐬𝐞𝐬 𝐭𝐨 𝐫𝐞𝐭𝐮𝐫𝐧 𝐢𝐭𝐬𝐞𝐥𝐟, 𝐈’𝐥𝐥 𝐭𝐫𝐮𝐬𝐭 𝐲𝐨𝐮 — 𝐧𝐨 𝐫𝐮𝐧𝐭𝐢𝐦𝐞 𝐜𝐡𝐞𝐜𝐤 𝐧𝐞𝐞𝐝𝐞𝐝.” Next time you chain methods in Java, remember — it’s Covariant Return Types working behind the scenes 🧠 #Java #Coding #SoftwareEngineering #OOP #CleanCode #TechInsights
Java's Covariant Return Types: A Hidden Gem for Fluent APIs
More Relevant Posts
-
🧠 Total Grid Ways Problem — Recursive Approach in Java 💻 Exploring how to find all possible paths from the top-left corner to the bottom-right corner in a grid — moving only right or down. This recursive solution breaks the problem into smaller subproblems, combining their results to find total unique paths. ✨ Sometimes, even a simple grid teaches us how powerful recursion can be!
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Just solved “Search in a Binary Search Tree” in Java with 100% runtime efficiency This problem was a great reminder of how elegant and efficient Binary Search Tree (BST) traversal can be when approached iteratively. 🔍 Key Idea: Start from the root and navigate left or right based on the target value — no recursion, just clean and optimized logic! 💡 Takeaway: Sometimes the simplest, most readable solutions are also the fastest. Writing clean and efficient code is a skill built through consistency and understanding the fundamentals. #Java #LeetCode #DSA #BinarySearchTree #CodingJourney #ProblemSolving #CleanCode
To view or add a comment, sign in
-
-
Day 89 of #100DaysOfCode Solved Find Numbers with Even Number of Digits in Java 🔢 Approach The goal of this problem was to count how many integers in a given array have an even number of digits. I implemented two methods: findNumbers(int[] nums): This is the main function that iterates through every number in the input array nums. isEvenOrOdd(int n): This helper function takes an integer and determines if its digit count is even or odd. Inside the helper function, I used a while loop to count the digits: I initialized a count to 0. The loop continues as long as $n > 0$. In each iteration, I increment count and then perform integer division by 10 (n = n / 10) to remove the least significant digit. Finally, I return true if the total count of digits is even (count \% 2 == 0). This efficient, digit-by-digit checking approach resulted in a strong performance, beating 98.90% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #ProblemSolving #Optimization
To view or add a comment, sign in
-
-
🚀 Day 18/100 of #100DaysOfCode ✅ Solved “Isomorphic Strings” (LeetCode #205) using Java! 🧩 Problem: Given two strings s and t, determine if they are isomorphic. 👉 Two strings are isomorphic if characters in one string can be replaced to get the other — while keeping order and unique mapping intact. 🧠 Approach: Used a HashMap to store the mapping of characters from s → t. Checked if each character in s has a consistent mapping in t. Also ensured that no two characters in s map to the same character in t. ✨ Example: s = "egg", t = "add" → true (e→a, g→d) s = "foo", t = "bar" → false 📈 Complexity: Time: O(n) Space: O(n) 💡 Key Learnings: Understood how to maintain one-to-one character mapping using a HashMap. Reinforced logic building for pattern matching between strings. 💻 Tech Used: Java | HashMap | Problem Solving #LeetCode #100DaysOfCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing #DevCommunity
To view or add a comment, sign in
-
-
🚀 Day 29 of 100 Days of LeetCode 📘 Problem: Combination Sum 💻 Language: Java ✅ Status: Accepted — Runtime ⚡ 2 ms (Beats 86%) Today’s problem was all about backtracking — exploring all possible combinations to reach a target sum. It’s a perfect blend of recursion, decision-making, and pruning unnecessary paths 🧠 ✨ Key Learnings: Backtracking is like exploring a maze — try, backtrack, and try again until you find the exit 🌀 Each recursive call represents a “choice point” — include or skip the element Clean recursion with controlled base cases leads to clarity and precision This problem reminded me how persistence works in both code and life: 💬 “Sometimes, the path to the solution is not straightforward — you just need to keep exploring.” #Day29 #100DaysOfCode #LeetCode #Java #Backtracking #Recursion #DSA #ProblemSolving #CodingJourney #SoftwareDevelopment #KeepLearning #CodeEveryday
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
📌 It’s surprising how the “small topics” end up revealing the biggest lessons. I recently revisited something I assumed I already understood… and it turned into one of the deepest dives I’ve taken into Java’s internals. Here’s what stood out: How the JDK uses static nested classes for performance and design clarity Why anonymous/local classes can shadow variables (and why lambdas won’t) How iterators, adapters, and strategy patterns rely heavily on nested class design And how avoiding hidden outer references actually improves memory + architecture The deeper I went, the more I understood how Java is engineered, not just how we use it. I’ve compiled everything into a clean, structured PDF with examples and explanations. 📄 Sharing the PDF here — if you're exploring Java in depth, this will be a valuable read. #Java #JavaDeveloper #SoftwareEngineering #CleanCode #BackendDevelopment
To view or add a comment, sign in
-
🚀 Day 26 of 100 Days of LeetCode 📘 Problem: Search a 2D Matrix 💻 Language: Java ✅ Status: Accepted — Runtime: ⚡ 0 ms (Beats 100%) Today’s problem focused on searching efficiently within a 2D matrix — a great exercise for understanding nested loops, traversal logic, and time optimization. While this version used a simple brute-force approach, it served as a good refresher on matrix iteration patterns before moving on to more optimized binary search techniques in 2D grids. ✨ Key Learnings: Matrix traversal can be intuitive when visualized 🔢 Always consider both brute-force and optimized approaches — understanding both is key to depth of knowledge Writing clean, readable code matters as much as optimizing it Each problem solved is another small brick in the foundation of strong problem-solving skills 💪 #Day26 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #SoftwareDevelopment #Matrix #KeepLearning #CodeEveryday
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode Solved Squares of a Sorted Array in Java 🔠 Approach The task was to take an array of integers sorted in non-decreasing order, square each number, and then return the result array also sorted in non-decreasing order. Brute-Force Method The solution implemented here is a straightforward two-step brute-force approach: Squaring: I iterated through the input array nums and replaced each element with its square (i.e., nums[i] * nums[i]). This handles both positive and negative numbers correctly. Sorting: After squaring all elements, I used Java's built-in Arrays.sort(nums) method to sort the entire array. While correct, this approach has a time complexity dominated by the sorting step, which is O(NlogN), where N is the number of elements. The runtime of 10 ms shows that a more efficient, two-pointer approach (which can solve this in O(N) time) is generally preferred for optimal performance. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #Sorting #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Great spotlight. Covariant returns are the unsung hero behind fluent builders—combine them with generics and you get chainable APIs without ugly casts. @Override keeps it safe, and classic examples (e.g., overriding clone() to return a subtype) show how much boilerplate they erase.