Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Abstract Keyword
Abstract Class
A class that cannot be instantiated. Can have both abstract (no body) and concrete (with body) methods.
java
QUBITS OF DPK
Abstract vs Interface
Inner Classes
java
QUBITS OF DPK
Types of Inner Classes
javascript
QUBITS OF DPK
Anonymous Inner Class
java
QUBITS OF DPK
Interfaces
What is an Interface?
A pure contract. Defines WHAT a class must do, not HOW.
java
QUBITS OF DPK
Java 8+ Interface Features
java
QUBITS OF DPK
Interface Variables
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Abstract classes provide partial abstraction — mix of abstract and concrete methods, can have constructors and state. Interfaces provide full contracts — all methods abstract by default (Java 8+ allows default/static). Key distinction: use abstract class for IS-A with shared code, interface for CAN-DO capability contracts. Interfaces solve multiple inheritance — a class can implement multiple interfaces. Anonymous inner classes let you create one-off implementations without a named class, common for callbacks and event listeners. Java 8 default methods in interfaces allowed adding new methods without breaking existing implementations."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between abstract class and interface?
Abstract class: can have abstract + concrete methods, instance variables of any type, constructors, and supports single inheritance. Interface: all methods abstract by default (Java 8+ allows default/static), variables are implicitly public static final, no constructors, supports multiple implementation. Abstract class for IS-A with shared state; interface for CAN-DO capability contracts.
Q2. When would you use abstract class vs interface?
Abstract class when: subclasses share significant code (template method pattern), you need constructors or instance state, you need non-public members. Interface when: you need multiple inheritance of type, you're defining a capability that unrelated classes can implement (Comparable, Serializable, Runnable), or you want to define a contract without any implementation. Rule of thumb: "is a" → abstract class; "can do" → interface.
Q3. Can an abstract class have a constructor?
Yes. Even though you can't instantiate an abstract class directly, its constructor is called when a concrete subclass is instantiated. The subclass constructor calls super() (implicitly or explicitly) which runs the abstract class constructor. This is used to initialize common fields shared by all subclasses:
java
QUBITS OF DPK
Q4. What is an anonymous inner class and when is it used?
An anonymous inner class is a class without a name, defined and instantiated in one expression. Used for one-time implementations of interfaces or abstract classes without creating a named class:
java
QUBITS OF DPK
Common uses: event listeners, callbacks, one-off strategy implementations. In Java 8+, lambda expressions replace most anonymous inner class usage for functional interfaces. Still used for non-functional interfaces (multiple methods).
Q5. What are default methods in interfaces (Java 8)?
Default methods are interface methods with a body, using the default keyword. They were added to allow adding new methods to existing interfaces without breaking all implementing classes (backward compatibility). Example: default void log() { System.out.println(this); } — all implementing classes automatically get this method unless they override it. Used extensively in Java Collections API (List.sort(), Map.forEach() etc.).
Q6. What happens if two interfaces have the same default method?
Compile error in the implementing class. Java forces you to resolve the conflict by overriding the method:
java
QUBITS OF DPK
Q7. Can an interface have variables? What type?
Yes, but they are implicitly public static final — constants. You cannot declare instance variables in an interface. int MAX = 100 in an interface is treated as public static final int MAX = 100. They must be initialized at declaration. This makes interfaces useful for defining shared constants, though using a dedicated constants class is generally preferred.
Q8. What is the need for interfaces in Java?
Four needs: (1) Multiple inheritance — a class can implement multiple interfaces, solving the diamond problem. (2) Loose coupling — code to an interface, not an implementation (List list instead of ArrayList list — switch to LinkedList without changing callers). (3) Contract definition — define what a class can do without dictating how. (4) Polymorphism across unrelated classes — Dog and Bird can both implement Flyable without sharing a common parent.