I'm always excited to take on new projects and collaborate with innovative minds.
say@bplbmki.in
https://www.bplbmki.in/
Java:
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
Dcallsshow(), should it invoke B's or C's version?
Java avoids this ambiguity by disallowing class-level multiple inheritance altogether.
The class implementing multiple interfaces had to provide all implementations, resolving any ambiguity itself.
interface A { void show(); }
interface B { void show(); }
class C implements A, B {
public void show() { System.out.println("Implemented"); }
}
➡️ No conflict because the class C provides the implementation.
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.
interface MyInterface {
void method1();
// Now if we add method2(), all old classes break!
}
default and static Methods to Interfaces in Java 8?| Reason | Explanation |
|---|---|
| ✅ API evolution without breaking existing implementations | If you add a new method to an interface, all implementing classes would break. Default methods solve this. |
| ✅ Avoid utility classes | Interfaces can now have behavior (e.g., log() method in Logger interface) without needing separate helper classes. |
| ✅ Promote cleaner multiple-inheritance of behavior | Multiple inheritance of logic is now possible in a controlled way—conflicts are resolved by implementing classes. |
| ✅ Enhance Functional Programming | Enables richer functional interfaces with reusable logic (Function, Predicate, etc.) |
| Question | Answer |
|---|---|
| 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 |
| Use Case | Abstract Class | Interface (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 |
Spring Boot:
✔️ 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>
✔️ A:
Spring Boot Actuator provides:
/actuator/metrics)/actuator/health).properties, YAML, or env vars without code changes.Enable it with:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
And expose endpoints: