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
1abstract class Shape {
2    String color;
3
4    abstract double area();           // no body — must override
5    abstract double perimeter();      // no body — must override
6
7    void displayColor() {             // concrete method
8        System.out.println("Color: " + color);
9    }
10}
11
12class Circle extends Shape {
13    double radius;
14
15    @Override
16    double area() { return Math.PI * radius * radius; }
17
18    @Override
19    double perimeter() { return 2 * Math.PI * radius; }
20}
21
22// Shape s = new Shape();   // ❌ cannot instantiate abstract!
23Shape s = new Circle();     // ✅ via polymorphism

Abstract vs Interface

Inner Classes

java
QUBITS OF DPK
1class Outer {
2    int outerValue = 10;
3
4    class Inner {               // Inner class
5        void display() {
6            System.out.println(outerValue);  // can access outer!
7        }
8    }
9}
10
11// Create inner object:
12Outer outer = new Outer();
13Outer.Inner inner = outer.new Inner();
14inner.display();   // 10

Types of Inner Classes

javascript
QUBITS OF DPK
11. Regular Inner Class      → inside class, needs outer instance
22. Static Nested Classstatic inside class, no outer instance needed
33. Local Inner Class        → inside a method
44. Anonymous Inner Class    → no name, defined and instantiated together

Anonymous Inner Class

java
QUBITS OF DPK
1// Instead of creating a separate class:
2Shape circle = new Shape() {
3    @Override
4    double area() { return 3.14 * 5 * 5; }
5
6    @Override
7    double perimeter() { return 2 * 3.14 * 5; }
8};
9
10// Used heavily for event listeners, callbacks
11button.addListener(new ClickListener() {
12    @Override
13    public void onClick() {
14        System.out.println("Clicked!");
15    }
16});

Interfaces

What is an Interface?

A pure contract. Defines WHAT a class must do, not HOW.
java
QUBITS OF DPK
1interface Flyable {
2    void fly();           // abstract by default
3    void land();
4}
5
6interface Swimmable {
7    void swim();
8}
9
10// Multiple interface implementation — solves multiple inheritance!
11class Duck extends Animal implements Flyable, Swimmable {
12    @Override public void fly()  { System.out.println("Duck flying"); }
13    @Override public void land() { System.out.println("Duck landing"); }
14    @Override public void swim() { System.out.println("Duck swimming"); }
15}

Java 8+ Interface Features

java
QUBITS OF DPK
1interface Vehicle {
2    void start();
3
4    // Default method — has body, can be overridden
5    default void stop() {
6        System.out.println("Vehicle stopped");
7    }
8
9    // Static method — utility methods
10    static int getWheelCount() { return 4; }
11}

Interface Variables

java
QUBITS OF DPK
1interface Constants {
2    int MAX_SPEED = 200;   // automatically public static final!
3}

️ All Traps

java
QUBITS OF DPK
1// Trap 1 — Abstract class cannot be instantiated
2new Shape();  // ❌ compile error
3
4// Trap 2 — All abstract methods must be implemented
5class Square extends Shape {
6    double area() { return side * side; }
7    // forgot perimeter()!  ❌ compile error
8}
9
10// Trap 3 — Interface variables are final
11interface I { int X = 10; }
12class C implements I { void m() { X = 20; } }  // ❌ X is final!
13
14// Trap 4 — Default method diamond problem in interfaces
15// If two interfaces have same default method — must override in class
16interface A { default void show() { System.out.println("A"); } }
17interface B { default void show() { System.out.println("B"); } }
18class C implements A, B {
19    @Override public void show() { A.super.show(); }  // ✅ resolve explicitly
20}

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
1abstract class Animal {
2    String name;
3    Animal(String name) { this.name = name; }  // runs when Dog is created
4}
5class Dog extends Animal {
6    Dog(String name) { super(name); }  // calls Animal constructor
7}
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
1Runnable r = new Runnable() {
2    @Override public void run() { System.out.println("Running!"); }
3};
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
1interface A { default void show() { System.out.println("A"); } }
2interface B { default void show() { System.out.println("B"); } }
3class C implements A, B {
4    @Override public void show() {
5        A.super.show();  // explicitly choose A's implementation
6    }
7}
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.