Once you understand classes and encapsulation, the next question becomes: How do we reuse behavior properly? That’s where 𝗶𝗻𝗵𝗲𝗿𝗶𝘁𝗮𝗻𝗰𝗲 comes in. Instead of rewriting common logic, Java allows one class to extend another. Example: 𝚌𝚕𝚊𝚜𝚜 𝚅𝚎𝚑𝚒𝚌𝚕𝚎 { 𝚟𝚘𝚒𝚍 𝚜𝚝𝚊𝚛𝚝() { 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝚅𝚎𝚑𝚒𝚌𝚕𝚎 𝚜𝚝𝚊𝚛𝚝𝚒𝚗𝚐"); } } 𝚌𝚕𝚊𝚜𝚜 𝙲𝚊𝚛 𝚎𝚡𝚝𝚎𝚗𝚍𝚜 𝚅𝚎𝚑𝚒𝚌𝚕𝚎 { 𝚟𝚘𝚒𝚍 𝚑𝚘𝚗𝚔() { 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗("𝙱𝚎𝚎𝚙"); } } Now 𝐂𝐚𝐫 automatically gets the 𝐬𝐭𝐚𝐫𝐭() behavior. This is powerful. But inheritance is not about copying code. It’s about expressing relationships. A 𝗖𝗮𝗿 𝗶𝘀 𝗮 𝗩𝗲𝗵𝗶𝗰𝗹𝗲. That phrase matters. Inheritance should represent: • Logical hierarchy • Shared behavior • Clear relationships When misused, it creates tight coupling and fragile systems. Today was about: • Understanding 𝗲𝘅𝘁𝗲𝗻𝗱𝘀 • The “is-a” relationship • When inheritance makes sense (and when it doesn’t) Reuse is important. But correct modeling is more important. #Java #OOP #Inheritance #SoftwareDesign #CleanCode #LearningInPublic
Java Inheritance: Understanding the 'is-a' Relationship
More Relevant Posts
-
𝐉𝐚𝐯𝐚 𝐢𝐬 𝐚𝐥𝐰𝐚𝐲𝐬 𝐏𝐚𝐬𝐬-𝐛𝐲-𝐕𝐚𝐥𝐮𝐞. 𝐒𝐨 𝐰𝐡𝐲 𝐝𝐢𝐝 𝐦𝐲 𝐚𝐫𝐫𝐚𝐲 𝐜𝐡𝐚𝐧𝐠𝐞? 🤔 I wrote a simple program to test how Java handles method arguments. [1] Declared an array 𝐚𝐫𝐫 in 𝐦𝐚𝐢𝐧. [2] Passed it to a 𝐜𝐡𝐚𝐧𝐠𝐞(𝐢𝐧𝐭[] 𝐧𝐮𝐦𝐬) method. [3] Modified 𝐧𝐮𝐦𝐬[𝟎] AND 𝐧𝐮𝐦𝐬[𝟏] inside the method. [4] The original 𝐚𝐫𝐫 was updated. 𝐓𝐡𝐞 𝐄𝐧𝐠𝐢𝐧𝐞𝐞𝐫𝐢𝐧𝐠 𝐄𝐱𝐩𝐥𝐚𝐧𝐚𝐭𝐢𝐨𝐧: Java is strictly Pass-by-Value. When I passed 𝐚𝐫𝐫, I didn't pass the object itself. I passed a copy of the reference variable. [A] Both the original 𝐚𝐫𝐫 and the method parameter 𝐧𝐮𝐦𝐬 hold the same memory address (pointing to the same array object in the Heap). [B] So, when 𝐧𝐮𝐦𝐬 modifies the data, it modifies the shared object. Understanding Stack (references) vs. Heap (objects) is crucial for avoiding bugs. #Java #MemoryManagement #SoftwareEngineering #CodingInterviews #LearningInPublic #BackendDevelopment #DataStructures #Coding #WeMakeDevs #100DaysOfCode #IntelliJ
To view or add a comment, sign in
-
-
Interfaces also allow a class to follow multiple contracts at the same time. Unlike classes, where a class can extend only one parent class, a class in Java can implement multiple interfaces. Things that became clear : • a class can implement more than one interface • each interface can define a different set of behaviours • the implementing class must provide implementations for all the methods • this approach allows combining different capabilities in a single class • it helps achieve flexibility while avoiding multiple inheritance of classes A simple example shows how multiple interfaces can be implemented : interface ICalculator { void add(int a, int b); void sub(int a, int b); } interface IAdvancedCalculator { void mul(int a, int b); void div(int a, int b); } class CalculatorImpl implements ICalculator, IAdvancedCalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } public void mul(int a, int b) { System.out.println(a * b); } public void div(int a, int b) { System.out.println(a / b); } } This structure allows the class to support operations defined by multiple interfaces while keeping responsibilities organized. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
🚀 Day 78 of- #100DaysOfCode Today’s problem: Check if the Sentence Is Pangram A pangram is a sentence that contains every letter of the English alphabet at least once. 🔹 Approach I Used: Loop from 'a' to 'z' Check if each character exists in the given string If any character is missing → return false If all characters are present → return true 💻 Java Solution: 🧠 Time Complexity: O(26 * n) → Since we check 26 letters and contains() takes O(n) Simplified: O(n) 📌 Space Complexity: O(1) (No extra data structure used) 💡 What I Learned: Sometimes simple brute-force solutions are clean and efficient enough. Always analyze time complexity even for small loops like 26 characters.
To view or add a comment, sign in
-
-
Another concept that appears while studying class initialization in Java is the instance block. It behaves differently from static blocks and is tied to object creation rather than class loading. Things that became clear : • an instance block runs every time an object of the class is created • it executes before the constructor • it can be used to perform common initialization steps for objects • unlike static blocks, instance blocks run for each object created • they are part of the object initialization process A simple structure shows the execution flow : class Demo { { System.out.println("Instance block executed"); } Demo() { System.out.println("Constructor executed"); } public static void main(String[] args) { Demo d = new Demo(); } } When the object is created, the instance block executes first and then the constructor runs. Understanding this order helps in seeing how Java prepares an object step by step during creation. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
Sometimes the value isn’t in building models — it’s in making them usable. I built an Image Classification library in Java using the Deep Java Library and a pretrained ResNet-50 model. The focus was not on training a model, but on designing a clean and reusable integration: • Simple service API for image classification • Proper exception handling and abstraction • Configurable via environment variables • Easy to plug into backend systems Working on this helped me understand that integrating ML into applications is less about algorithms and more about designing reliable and maintainable systems around them. https://lnkd.in/gQ6dQEtQ #Java #DJL #MachineLearning #BackendDevelopment #SoftwareEngineering #AIinJava
To view or add a comment, sign in
-
🚀 Day 8 — Restarting My Java Journey with Consistency Today’s topic looked familiar: 🔹 while loop 🔹 do-while loop 🔹 for loop 🔹 break & continue Most of this was already known to me. But revision with depth always reveals something new. 🔁 Loops — More Than Just Repetition We often write: for(int i = 0; i < n; i++) { // code } But today I revisited some important insights: ✔ All three parts in a for loop are optional ✔ Multiple variables can be initialized using comma separation ✔ Conditional statements rely completely on logical operators ✔ do-while is very useful in menu-driven programs (runs at least once) 🤯 Interesting Question Why don’t we usually use short instead of int in loops? Because in Java, due to type promotion, short gets promoted to int during arithmetic operations. So practically, using short in loops doesn’t provide any benefit. That’s not syntax knowledge. That’s understanding how Java works internally. 🆕 The New Concept I Learned — Labels in Java This was something I had never used before. outer: for(int i = 1; i <= 10; i++) { inner: for(int j = 1; j <= i; j++) { break outer; // breaks the outer loop directly } } 🔹 Labels allow us to control outer loops from inside inner loops 🔹 Useful in nested loop scenarios 🔹 Makes flow control very powerful (if used wisely) Learning daily with Coder Army and Aditya Tandon Bhaiya and Rohit Negi Bhaiya #Day8 #Java #Consistency #BackendDevelopment #LearningJourney #SoftwareEngineering #CoderArmy
To view or add a comment, sign in
-
-
𝐉𝐚𝐯𝐚 𝐌𝐞𝐭𝐡𝐨𝐝𝐬 𝐂𝐡𝐞𝐚𝐭 𝐒𝐡𝐞𝐞𝐭 𝐟𝐨𝐫 𝐃𝐒𝐀 While learning, remembering important Java methods saves a lot of time. So I created a quick Java Cheat Sheet focused only on methods that are useful. 𝐂𝐨𝐯𝐞𝐫𝐬: 🔹 String Methods ✔️ length() ✔️ charAt() ✔️ substring() ✔️ indexOf() ✔️ toCharArray() 🔹 Arrays Class ✔️ sort() ✔️ fill() ✔️ equals() ✔️ binarySearch() 🔹 Collections & List ✔️ add() ✔️ remove() ✔️ get() ✔️ set() ✔️ contains() ✔️ size() 🔹 HashMap Methods ✔️ put() ✔️ get() ✔️ containsKey() ✔️ keySet() ✔️ entrySet() 🔹 Stack / Queue Methods ✔️ push() ✔️ pop() ✔️ peek() ✔️ offer() ✔️ poll() #Javadeveloper #softwaredeveloper #coding #javamethods #corejava
To view or add a comment, sign in
-
Quick Sort in Java may look complex, but most mistakes happen because: • we don’t clearly understand how the pivot divides the array • we mix up the partition logic • we think recursion is doing the sorting The core idea is straightforward: choose a pivot, place it in the correct position, and recursively apply the same process to the left and right parts. What really happens: – A pivot element is selected – The array is rearranged so that elements smaller than pivot go to the left – Elements greater than pivot go to the right – The pivot lands at its final sorted position Then: – The left subarray is partitioned again using a new pivot – The right subarray is partitioned again – This continues until subarrays have 0 or 1 element – A single element is already sorted by definition So: – First, one pivot divides the array into two parts – Then each part is divided again – Then those smaller parts are divided further – And so on, until everything is positioned correctly The key insight: 👉 All the real sorting happens during partitioning, not after recursion finishes. Once you understand that: • recursion just keeps reducing the problem size • partition logic ensures correct positioning • average time complexity stays O(n log n) Quick Sort isn’t about guaranteed worst-case performance — it’s about speed, in-place efficiency, and practical performance. That’s what makes it a foundation algorithm for: ✔ in-memory sorting ✔ competitive programming ✔ high-performance systems #Java #QuickSort #DSA #Algorithms #DivideAndConquer #ProblemSolving #BackendEngineering
To view or add a comment, sign in
-
Day 32 of #100DaysOfLeetCode 💻✅ Solved #144. Binary Tree Preorder Traversal on LeetCode using Java. Approach: • Used recursion to perform preorder traversal • Followed Root → Left → Right order strictly • Added node value before traversing subtrees • Traversed left subtree first, then right subtree • Stored values in a list during traversal Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) ✓ Memory: 42.83 MB (Beats 96.77% submissions) Key Learning: ✓ Strengthened understanding of preorder traversal pattern ✓ Clearly differentiated preorder from inorder traversal ✓ Improved recursive tree traversal implementation skills Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinaryTree #Recursion #TreeTraversal #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 LeetCode Problem Solved: 454. 4Sum II Today I solved 4Sum II (Medium) on LeetCode using an optimized approach with HashMap in Java. 🔹 Problem Summary: Given four integer arrays nums1, nums2, nums3, and nums4, count the number of tuples (i, j, k, l) such that: nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 🔹 Approach Used: Instead of using brute force (O(n⁴)), I optimized it to O(n²) using a HashMap: 1️⃣ Store all possible sums of nums1[i] + nums2[j] in a HashMap with frequency. 2️⃣ For each pair from nums3[k] + nums4[l], check if -(sum) exists in the map. 3️⃣ Add the frequency to the count. 💡 This reduces time complexity from O(n⁴) → O(n²), which is a huge improvement for large inputs. 🔹 Key Concepts Practiced: ✔ HashMap ✔ Frequency counting ✔ Time complexity optimization ✔ Thinking in terms of pair sums Consistent DSA practice is helping me improve my problem-solving patterns and optimization mindset. 💪 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
Explore related topics
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