Revisiting core Java interview concepts often reveals interesting edge cases. Here’s one worth thinking about. What will be the output of the following Java code? class A { static void test() { System.out.println("A"); } } class B extends A { static void test() { System.out.println("B"); } } A a = new B(); a.test(); How would you approach this? Share your reasoning in the comments. #Java #OOPs #Polymorphism #JavaInterview #Coding #Programming #SoftwareTesting
Java Polymorphism Edge Case: Overriding Static Methods
More Relevant Posts
-
🚨 One Java Interview Question Many Developers Still Get Wrong equals() vs hashCode() Most developers know these methods exist in Java, but many don’t fully understand why they must work together. Let’s break it down 👇 🔹 equals() • Used to compare the logical equality of two objects • By default, it compares memory references, not values • We override it when we want to compare object content 🔹 hashCode() • Returns an integer hash value for the object • Used internally by HashMap, HashSet, and HashTable • Helps Java quickly locate objects in hash buckets ⚠️ Important Rule If two objects are equal using equals(), they must return the same hashCode(). Otherwise, collections like HashSet or HashMap may behave incorrectly. 💡 Example Issue You add two logically equal objects into a HashSet, but because their hashCode values are different, both get stored. Result? Duplicate objects inside a Set. ✅ Takeaway Whenever you override equals(), always override hashCode() as well. This small rule can prevent subtle and hard-to-debug issues in real applications. 💬 Have you ever faced a bug related to equals() and hashCode()? #Java #SoftwareDevelopment #Programming #JavaDeveloper #CodingInterview #BackendDevelopment
To view or add a comment, sign in
-
❗Why is the 𝗺𝗮𝗶𝗻() method static in Java? 🤔 This is something many of us memorize, but don’t really pause to think about. 😅 Here’s the simple idea 👇 When you run a Java program, the JVM needs a starting point. That starting point is the "main()" method. Now the important part 👇 ➡️ The JVM doesn’t create objects to start execution ➡️ It directly calls the method using the class name So if "main()" was not static, JVM would need to: ❌ Create an object first ❌ Then call "main()" But… it can’t do that without already starting the program. That’s exactly why "main()" is static. 👉 Simple: 𝐉𝐕𝐌 𝐬𝐭𝐚𝐫𝐭𝐬 𝐞𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐰𝐢𝐭𝐡𝐨𝐮𝐭 𝐜𝐫𝐞𝐚𝐭𝐢𝐧𝐠 𝐚𝐧 𝐨𝐛𝐣𝐞𝐜𝐭, 𝐬𝐨 𝐦𝐚𝐢𝐧() 𝐦𝐮𝐬𝐭 𝐛𝐞 𝐬𝐭𝐚𝐭𝐢𝐜 𝐭𝐨 𝐛𝐞 𝐜𝐚𝐥𝐥𝐞𝐝 𝐮𝐬𝐢𝐧𝐠 𝐭𝐡𝐞 𝐜𝐥𝐚𝐬𝐬 𝐧𝐚𝐦𝐞. Sometimes, these small concepts are what interviewers are really looking for. Was this something you already knew, or did you just memorize it before? 🤔 Let’s discuss 👇💬 #Java #JavaDeveloper #JavaBackend #TechJourney #LearnBySharing #Programming #JavaConcepts #JVM #InterviewPrep
To view or add a comment, sign in
-
☁☕ Java Core Concepts – Interview Question 📌 Give some features of an Interface In Java, an Interface is an abstract type used to define a contract (behavior) that classes must follow. 🔹 Key Features of Interface: ✔ Provides 100% abstraction (by default) ✔ Contains abstract methods and static constants ✔ Supports multiple inheritance (a class can implement multiple interfaces) ✔ Enables loose coupling between classes ✔ Helps achieve polymorphism ✔ Methods are public and abstract by default ✔ Variables are public, static, and final by default 🔹 Additional Points: • A class uses implements keyword to inherit an interface • From Java 8+, interfaces can have default and static methods 💡 In Short: Interfaces act as a blueprint for behavior, helping build flexible, scalable, and loosely coupled applications. 👉For Java Course Details Visit : https://lnkd.in/gwBnvJPR . #Java #CoreJava #Interface #JavaInterview #Programming #Coding #TechSkills #Ashokit
To view or add a comment, sign in
-
-
Today I Learned: Static vs Non-Static in Java — Order of Execution While revising core Java, I finally got a clear understanding of how the JVM executes static and non-static members. This topic looks simple, but it’s one of the most asked interview concepts! 💡 Key Takeaways: 🔹 Static members belong to the class Static variables load first Static blocks run once when class loads main() starts after static initialization 🔹 Non-static members belong to the object Instance variables load during object creation Non-static blocks run before constructor Constructor initializes the object Instance methods run when called 🔥 Execution Flow Simplified Class Loading → Static Vars → Static Block → main() → Object Creation → Instance Vars → Init Block → Constructor → Methods #Java #JavaProgramming #JavaDeveloper #SoftwareDevelopment #Programming #Coding #BackendDevelopment #TechLearning #Developers #LearnToCode #ProgrammingCommunity #100DaysOfCode #CodeNewbie #TechCareer #SoftwareEngineer
To view or add a comment, sign in
-
-
Java Streams are one of the most powerful features in modern Java — and also one of the most common interview topics. Question 1: Find the Second Highest Number using Java Stream API. Input: [10, 40, 20, 50, 30] Output: 40 Key Stream operations used: • stream() • distinct() • sorted() • skip() • findFirst() These small patterns appear frequently in Java coding interviews. Save this post for your next Java interview preparation. Next post → Question 2 #Java #JavaStreams #CodingInterview #BackendDevelopment #SoftwareEngineering #JavaDeveloper #Programming #TechLearning
To view or add a comment, sign in
-
-
☕ Java Core Concepts – Interview Question 📌 What is Runtime (Dynamic) Polymorphism? In Java, Runtime Polymorphism (also called Dynamic Method Dispatch) is a concept where the method to be executed is determined at runtime, not at compile time. 🔹 Key Points: ✔ Achieved through Method Overriding ✔ Method call is resolved during execution ✔ Depends on the object type, not reference type 🔹 How it Works: • A parent class reference points to a child class object • The overridden method in the child class is executed 🔹 Why it’s Important: ✔ Enables flexibility and extensibility ✔ Supports runtime decision making ✔ Improves code reusability 💡 In Short: Runtime polymorphism allows Java to decide which method to call at runtime, based on the actual object, enabling dynamic behavior in applications. 👉For Java Course Details Visit :https://lnkd.in/gwBnvJPR . #Java #CoreJava #Polymorphism #JavaInterview #Programming #Coding #TechSkills
To view or add a comment, sign in
-
-
Earlier I thought I knew OOPS in Java… But while solving output-based questions, I realized the real depth. 🔹 Variables → Compile-time binding (depends on reference) 🔹 Overridden methods → Runtime binding (depends on object) 🔹 Static methods → Compile-time (method hiding) That’s when I understood — Polymorphism is not just theory, it’s about how JVM actually decides what to execute. Now output-based interview questions feel much clearer 💯 Still learning. Still improving. #Java #OOPS #BackendDeveloper #InterviewPreparation #Learning
To view or add a comment, sign in
-
🚨 Java Interview Trap Can the 'main()' method be "overloaded" in Java? Most developers quickly answer No. The `main()` method is just another static method, so Java allows method overloading. Example: public class Test { public static void main(String[] args) { System.out.println("Original main"); main(10); } public static void main(int x) { System.out.println("Overloaded main with int: " + x); } } Output: Original main Overloaded main with int: 10 However, there is an important rule. The JVM only looks for one specific entry point: public static void main(String[] args) Or public static void main(String... args) So even though main() can be overloaded, the JVM will only start execution from the standard signature. Any other overloaded main() must be called manually from inside the program. Sometimes the most basic Java method still hides the best interview traps. #Java #JavaInterview #JVM #SoftwareEngineering #Developers #Programming
To view or add a comment, sign in
-
🚀 Java Interview Question Series What is the difference between HashMap and ConcurrentHashMap? HashMap: ❌ Not thread safe ❌ Multiple threads can modify data simultaneously ConcurrentHashMap: ✅ Thread safe ✅ Allows concurrent read/write operations Used in multithreaded environments. Many developers fail this simple interview question. Follow me for more Java interview questions and coding tips. 💻 #Java #Programming #CodingInterview #SoftwareDevelopment
To view or add a comment, sign in
-
❗Why is the 𝗺𝗮𝗶𝗻() method static in Java? 🤔 This is something many of us memorize, but don’t really pause to think about. 😅 Here’s the simple idea 👇 When you run a Java program, the JVM needs a starting point. That starting point is the "main()" method. Now the important part 👇 ➡️ The JVM doesn’t create objects to start execution ➡️ It directly calls the method using the class name So if "main()" was not static, JVM would need to: ❌ Create an object first ❌ Then call "main()" But… it can’t do that without already starting the program. That’s exactly why "main()" is static. 👉 Simple: 𝐉𝐕𝐌 𝐬𝐭𝐚𝐫𝐭𝐬 𝐞𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐰𝐢𝐭𝐡𝐨𝐮𝐭 𝐜𝐫𝐞𝐚𝐭𝐢𝐧𝐠 𝐚𝐧 𝐨𝐛𝐣𝐞𝐜𝐭, 𝐬𝐨 𝐦𝐚𝐢𝐧() 𝐦𝐮𝐬𝐭 𝐛𝐞 𝐬𝐭𝐚𝐭𝐢𝐜 𝐭𝐨 𝐛𝐞 𝐜𝐚𝐥𝐥𝐞𝐝 𝐮𝐬𝐢𝐧𝐠 𝐭𝐡𝐞 𝐜𝐥𝐚𝐬𝐬 𝐧𝐚𝐦𝐞. Sometimes, these small concepts are what interviewers are really looking for. Was this something you already knew, or did you just memorize it before? 🤔 Let’s discuss 👇💬 hashtag #Java hashtag #JavaDeveloper hashtag #JavaBackend hashtag #TechJourney hashtag #LearnBySharing hashtag #Programming hashtag #JavaConcepts hashtag #JVM hashtag #InterviewPrep
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