Java Concept: Constructor Chaining in Inheritance Looks like a simple code, but it’s one many developers overlook class A { A() { System.out.println("A constructor"); } } class B extends A { B() { System.out.println("B constructor"); } } public class Test { public static void main(String[] args) { new B(); } } Explanation: When new B() is called — Java first calls A’s constructor using an implicit super() call. Then executes B’s constructor. ✅ Output: A constructor B constructor #Java #OOPs #CodingTips #LearningJava #InterviewReady #Developers
How Constructor Chaining Works in Java Inheritance
More Relevant Posts
-
🚀 Day 4 — The Java Trick That Even Seniors Miss! 😮 Every Java dev thinks they understand method overloading... But this one small detail breaks their confidence 👇 public class OverloadTest { void show(Object o) { System.out.println("Object"); } void show(String s) { System.out.println("String"); } public static void main(String[] args) { OverloadTest test = new OverloadTest(); test.show(null); } } 🧠 Question: What will be the output of this code? Will it print: 1️⃣ Object 2️⃣ String 3️⃣ Compile-time error Most developers get this wrong! 😅 💬 Drop your answer in the comments 👇 Let's see how many of you actually know how Java resolves overloaded methods at compile time! #Java #CodingChallenges #SpringBoot #JavaDevelopers #InterviewQuestion #Day4Challenges #cbfr
To view or add a comment, sign in
-
💡 Difference between == and .equals() in Java — and why it still confuses even experienced devs In Java, many developers think == and .equals() do the same thing... but they don’t 👇 ⚙️ == — The == operator compares memory references. It checks whether two variables point to the same object. String a = new String("Java"); String b = new String("Java"); System.out.println(a == b); // false 🚫 Here, a and b are two different objects, even if their content is identical. 🧠 .equals() — The .equals() method compares the logical content of the objects (when properly implemented). System.out.println(a.equals(b)); // true ✅ Both Strings contain “Java”, so the result is true. 🧩 Extra tip: Primitive types (int, double, boolean, etc.) use == because they don’t have .equals(). Objects (String, Integer, List, etc.) should use .equals() unless you need to check if they’re the same object in memory. 💬 Conclusion: Use == ➡️ to compare references Use .equals() ➡️ to compare values 💭 Have you ever fallen into this trap? Share your experience below 👇 #Java #Backend #CleanCode #DeveloperTips #SpringBoot #Programming #Learning
To view or add a comment, sign in
-
🚀 Day 1 — The Java Memory Illusion 💭 Every Java developer thinks they know how memory works… But 95% fail this simple-looking question 👇 𝐒𝐭𝐫𝐢𝐧𝐠 𝐬𝟏 = "𝐉𝐚𝐯𝐚"; 𝐒𝐭𝐫𝐢𝐧𝐠 𝐬𝟐 = 𝐬𝟏.𝐜𝐨𝐧𝐜𝐚𝐭("𝐑𝐨𝐜𝐤𝐬"); 𝐒𝐭𝐫𝐢𝐧𝐠 𝐬𝟑 = 𝐬𝟏 + "𝐑𝐨𝐜𝐤𝐬"; 𝐒𝐭𝐫𝐢𝐧𝐠 𝐬𝟒 = "𝐉𝐚𝐯𝐚𝐑𝐨𝐜𝐤𝐬"; 𝐈𝐧𝐭𝐞𝐠𝐞𝐫 𝐚 = 𝟏𝟎𝟎; 𝐈𝐧𝐭𝐞𝐠𝐞𝐫 𝐛 = 𝟏𝟎𝟎; 𝐈𝐧𝐭𝐞𝐠𝐞𝐫 𝐜 = 𝟐𝟎𝟎; 𝐈𝐧𝐭𝐞𝐠𝐞𝐫 𝐝 = 𝟐𝟎𝟎; 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧(𝐬𝟐 == 𝐬𝟑); 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧(𝐬𝟑 == 𝐬𝟒); 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧(𝐚 == 𝐛); 𝐒𝐲𝐬𝐭𝐞𝐦.𝐨𝐮𝐭.𝐩𝐫𝐢𝐧𝐭𝐥𝐧(𝐜 == 𝐝); Looks easy, right? 😏 But only one of these comparisons behaves exactly how you expect! 💭 Before you scroll... 👉 Which of these return true and which return false? 👉 What’s happening inside the String Constant Pool and Integer Cache? 👉 Why does the compiler optimize + concatenation differently from .concat()? 🧩 Your Challenge: Comment below 👇 with your exact outputs AND the JVM-level explanation behind each one. No guessing. Only real memory-level logic. 💡 Let’s see who truly understands how Java handles Strings and Wrappers under the hood. 🔥 #Java #ProgrammingChallenges #CoreJava #MemoryManagement #Developers #CodingChallenge #TechCommunity #JVM #LearnJava #Dailycodings #Javadevelopers
To view or add a comment, sign in
-
☕ Java Execution Made Simple Have you ever wondered how your Java code actually runs behind the scenes? Let’s break it down step by step 👇 🧩 1️⃣ Source Code (.java) You write code in your IDE — it’s human-readable and logical. 👉 Example: System.out.println("Hello Java!"); ⚙️ 2️⃣ Java Compiler (javac) It converts your .java file into a .class file — called bytecode. 🗂️ Bytecode isn’t tied to any OS or processor. 📦 3️⃣ Bytecode (.class) This is platform-independent. You can run (Java fileName) it on any system that has JVM — that’s Java’s “write once, run anywhere” magic! ✨ 🧠 4️⃣ JVM (Java Virtual Machine) JVM takes care of everything at runtime: Class Loader → Loads classes Bytecode Verifier → Checks safety Interpreter → Executes bytecode line by line 🚀 5️⃣ JIT Compiler (Just-In-Time) JIT notices which parts of your code run frequently (called hotspots). It then converts those into machine code for faster execution. ⚡ 6️⃣ Cached Execution Next time the same code runs, JVM uses the cached native code — making it super fast! -- #Java #LearningTogether #CodingSimplified #ProgrammingTips #JVM #SoftwareEngineering
To view or add a comment, sign in
-
Java Tricky Question Most developers think they know what finally does… until they see this. What do you think this program prints? class Test { public static void main(String[] args) { System.out.println(test()); } static int test() { try { return 10; } finally { return 20; } } } Hint: finally always executes but does it override the value returned from try? #Java #CodingChallenge #Developers #ProgrammingPuzzle #JavaTrickyQuestions #LearnJava #BackendDeveloper
To view or add a comment, sign in
-
-
Most Java developers think they understand exception handling… until they look at what the JVM actually does under the hood. One of the most surprising things about the JVM is that exception handling is not implemented as simple jumps. The JVM runs normal code without any built-in exception checks and instead relies on a completely separate exception-mapping structure. Only when an exception occurs does the JVM consult this structure to find the correct handler. This design keeps normal execution fast, but it also explains many of the “weird” behaviours we see when debugging or reading decompiled code. For example: • finally blocks are often duplicated across every possible exit path • Almost any instruction can potentially throw, so try ranges cover more than you expect • Nested try/catch blocks turn into overlapping, non-nested bytecode regions • try-with-resources generates some of the most complex exception paths in the JVM • Decompilers struggle because they only see bytecode ranges, not the original structured blocks The source code looks clean, but the compiled form is far more intricate. Understanding this gives developers a deeper appreciation for how the JVM balances performance, safety, and flexibility. #java #corejava
To view or add a comment, sign in
-
🧠 Day-19: OOPs — Interfaces in Java ✅ What is an Interface? An interface is nothing but a blueprint of a class. It specifies what a class must do, but not how it does it. 🔹 Relationships Class to Class → extends Interface to Interface → extends ✅ (not implements) Class to Interface → implements ✅ Interface to Class → ❌ Not allowed 🔹 Key Points In Java 1.7 and earlier, interfaces provided 100% abstraction — meaning they could only contain abstract methods (no method body). From Java 8 onward, interfaces can include: default methods (with body) static methods (with body) All interface methods are implicitly public and abstract (unless declared default or static). All variables in an interface are public, static, and final by default. 10000 Coders #Java #Interface #OOPs #LearningEveryday #100DaysOfCode #Day19 #CodingJourney
To view or add a comment, sign in
-
-
⚠️ Why NullPointerException Still Happens (Even in 2025) — And How to Actually Avoid It 🧠 We’ve all seen this: java.lang.NullPointerException Java’s most infamous runtime exception 😅 But here’s the truth — NPEs don’t happen because Java is bad. They happen because developers don’t control their object flow. Let’s fix that 👇 --- 🔹 1️⃣ The Real Reason NPE Happens Most developers think NPE happens when: > “The variable is null.” But the real root cause is: 👉 A broken object graph. Your object wasn’t created, injected, or initialized at the right time. Example👇 user.getAddress().getCity(); One missing link in the chain → boom 💥 NPE. --- 🔹 2️⃣ Hidden Places Where NPE Sneaks In You’ll be surprised where NPEs often come from: ❌ Uninitialized fields ❌ Missing dependency injection (Spring beans not created) ❌ Bad DTO design ❌ Optional misused ❌ Returning null from utility methods ❌ Map lookups returning null ❌ Missing default values Most NPEs are NOT about “null” — they’re about incomplete data flow. --- 🔹 3️⃣ How to Actually Avoid NPE (Real Solutions, Not Myths) ✔ Use constructor-based dependency injection If your object needs something, force it through the constructor. No more half-initialized beans. ✔ Return Optional instead of null But only when it makes sense — don’t abuse it. ✔ Validate inputs early Fail fast. Don’t allow nulls to travel through 5 layers of code. ✔ Use defensive defaults list = list == null ? List.of() : list; ✔ Avoid deep chains Instead of a.getB().getC().getD() break it down → more readable, safer. --- ⚡ The Takeaway NullPointerException isn’t a Java problem — it’s a design problem. Fix your object lifecycle, and NPE disappears. --- 💬 Your turn: Be honest — how many NPEs have you debugged in your career? 😅 👇 And what was the funniest cause you found? #Java #ErrorHandling #NPE #JavaDeveloper #CleanCode #BackendDevelopment #SoftwareEngineering #SpringBoot
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