I'm always excited to take on new projects and collaborate with innovative minds.

Mail

say@bplbmki.in

Website

https://www.bplbmki.in/

javafaq

Java:

❓ Why Multiple Inheritance is Not Allowed for Classes in Java (Till Java 7 and Beyond)

✅ Problem: Diamond Problem

Imagine this scenario:

class A { void show() { System.out.println("A"); } }
class B extends A { void show() { System.out.println("B"); } }
class C extends A { void show() { System.out.println("C"); } }

// Now:
// class D extends B, C — This causes ambiguity!
 

Ambiguity: If D calls show(), should it invoke B's or C's version?

Java avoids this ambiguity by disallowing class-level multiple inheritance altogether.

 

✅ Why Multiple Interface Inheritance is Allowed (Even Before Java 8)

➡️ No conflict because the class C provides the implementation.

❓ Why Static and Default Methods Were Introduced in Java 8?

✅ Motivation: Backward Compatibility for Evolving APIs

Let’s say Java added a method to an existing interface (like List in the JDK).
Before Java 8, all implementing classes would break because they’d now be forced to implement this new method.

🧩 Example Problem (Pre-Java 8):

interface MyInterface {
   void method1();
   // Now if we add method2(), all old classes break!
}
 

✅ Solution in Java 8: Default Methods

interface MyInterface {
   void method1();

   default void method2() {
       System.out.println("Default Implementation");
   }
}
✅ Now older classes don’t need to implement method2() unless they want to override it.

❓ Why Are Static Methods Allowed in Interfaces (Since Java 8)?

✅ Reason: Utility Methods Scoped to Interface

Before Java 8:

  • You had to write utility classes like Collections, Arrays, etc., to group static methods.

Now:

  • You can define static methods directly inside the interface itself.
  • These are not inherited, hence no ambiguity.

interface Utility {
   static void log() {
       System.out.println("Logging...");
   }
}
Utility.log(); // Valid, no object needed
 

FeatureAllowedWhy / Why Not
Multiple class inheritance❌ NoDiamond problem, ambiguity in method resolution
Multiple interface inheritance (abstract)✅ YesNo method body → no conflict
Interface default method✅ Since Java 8Needed for backward-compatible API evolution
Interface static method✅ Since Java 8For logical grouping of utility methods inside interface
Class with multiple interfaces with default methods✅ Yes, but must override in case of conflictJava requires conflict resolution explicitly

✅ Quick Recap

ConceptAbstract ClassInterface (Java 7)Interface (Java 8+)
Can have abstract methods?✅ Yes✅ Yes✅ Yes
Can have concrete methods?✅ Yes❌ No✅ Yes (via default, static)
Can have constructors?✅ Yes❌ No❌ No
Can have fields?✅ Yes (any type)✅ Only public static final 
Supports multiple inheritance?❌ No✅ Yes✅ Yes
Used for?Partial abstractionTotal abstraction (Java 7) → Evolving APIs (Java 8+)API Evolution
Can have static methods?✅ Yes❌ No✅ Yes
Can have default methods?❌ No❌ No✅ Yes

❓ Why Add default and static Methods to Interfaces in Java 8?

ReasonExplanation
API evolution without breaking existing implementationsIf you add a new method to an interface, all implementing classes would break. Default methods solve this.
Avoid utility classesInterfaces can now have behavior (e.g., log() method in Logger interface) without needing separate helper classes.
Promote cleaner multiple-inheritance of behaviorMultiple inheritance of logic is now possible in a controlled way—conflicts are resolved by implementing classes.
Enhance Functional ProgrammingEnables richer functional interfaces with reusable logic (Function, Predicate, etc.)

❓ Can't We Do This With Abstract Class?

QuestionAnswer
Can abstract class have abstract + concrete methods?✅ Yes
Can it have static methods?✅ Yes
Can it solve API evolution problems?❌ No – Java allows only single inheritance for classes, so can't evolve multiple base APIs via abstract class
Can a class inherit multiple abstract classes?❌ No (Java doesn't support multiple class inheritance)
Can abstract class be used for utilities?❌ Not ideal – utility classes are better as final with static methods

✅ Why Interfaces with Default/Static Methods were Necessary

Use CaseAbstract ClassInterface (Java 8+)
API needs version upgrade without breaking existing clients❌ Not possible — can't change abstract class hierarchy easily across multiple types✅ Add default methods
Need multiple inheritance of behavior❌ Not allowed in Java✅ Multiple interfaces with default methods allowed
Share utility code across implementations✅ Static methods✅ Static methods (grouped under the interface name)
Define a functional contract (lambda-friendly)❌ Verbose✅ With @FunctionalInterface, default methods, lambdas

 

Abstract Class (Pre-Java 8)

abstract class Animal {
   abstract void sound();
   void walk() { System.out.println("Walking"); } // Concrete method
   static void info() { System.out.println("Animal Info"); }
}
 

Interface with Default + Static (Java 8+)

interface Animal {
   void sound();

   default void walk() {
       System.out.println("Walking");
   }

   static void info() {
       System.out.println("Animal Info");
   }
}
 

✅ Can We Write Default or Static Methods in Abstract Class?

FeatureAbstract Class
default keyword❌ Not applicable. Use normal method instead
static method✅ Yes
Concrete method✅ Yes
default is a keyword specific to interfaces only (since Java 8). In abstract classes, we just write a normal concrete method—no need for default.
 

 

 

 

 

 

 

Spring Boot:

Q1: How do I change the default embedded server (Tomcat) in Spring Boot to Jetty or Undertow?

✔️ A:
Exclude Tomcat from spring-boot-starter-web and add your preferred server like this (for Jetty):

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <exclusions>
   <exclusion>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
   </exclusion>
 </exclusions>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
 

Q2: What are Spring Boot Actuator features useful for production?

✔️ A:
Spring Boot Actuator provides:

Enable it with:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
 

And expose endpoints:

management.endpoints.web.exposure.include=health,metrics
 

  • http://localhost:8080/actuator/health – Health status
  • http://localhost:8080/actuator/metrics – All available metrics
  • http://localhost:8080/actuator/metrics/jvm.memory.used – Specific metric
  • Metrics (memory, threads, DB, HTTP requests)
  • Health Checks (DB, disk space, services)
  • Externalized Configuration (app settings via files/env)

 

IdempotentSame effect every time

📌 Doing it once or 100 times = same result

🧠 Example:

  • Locking a door
    → If the door is already locked, locking again does nothing.

  • Setting your phone volume to 50%
    → Even if you set it again and again, it stays at 50%.

Non-IdempotentDifferent effect every time

📌 Doing it again = different result

🧠 Example:

  • Adding money to your wallet
    → ₹100 + ₹100 = ₹200
    → Every time you do it, the balance increases.

  • Sending a message
    → If you send it twice, the person gets it twice.