Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Lesson 8: Interfaces — default/static Methods, Functional & Marker Interfaces

Lesson 9 of 18 in the free Programming in Java notes on Siksha Sarovar, written by Rohit Jangra.

8.1 What an Interface Really Is

An interface is a pure contract: a type that says what an implementer can do, with no instance state. Implicit modifiers examiners test:

  • Fields are always public static final (constants) — you cannot have instance variables.
  • Abstract methods are always public abstract (writing them is optional/redundant).
  • Interfaces have no constructors and cannot be instantiated; a class implements them, an interface extends other interfaces (even several at once).
interface Payable {
    double TAX = 0.18;                    // public static final implicitly
    double amountDue();                   // public abstract implicitly
}

A class may implements many interfaces — this is Java's multiple inheritance of type. It is safe because interfaces carry no state; only behaviour can conflict, and conflicts must be resolved explicitly (§8.3).

8.2 Evolution: default, static & private Methods

Member kindSincePurposeCalled via
Abstract method1.0The contractImplementing object
default method8Evolve interfaces without breaking implementersObject (inheritable, overridable)
static method8Utility helpersInterfaceName.method() only — not inherited
private method9Share code between defaultsInside the interface only

default methods exist for backward compatibility: when Java 8 added forEach to Iterable, every existing collection class would have broken without a default body.

8.3 The Diamond Problem — and Java's Resolution Rules

interface A { default String hi() { return "A"; } }
interface B { default String hi() { return "B"; } }
class C implements A, B {                 // conflict! compiler FORCES an override
    @Override public String hi() {
        return A.super.hi() + B.super.hi();   // explicitly pick/combine parents
    }
}

Resolution rules, in order: (1) a superclass method always beats interface defaults ("class wins"); (2) the most specific sub-interface default wins; (3) otherwise the class must override and may delegate with X.super.method().

8.4 Functional Interfaces — the Lambda Target

A functional interface has exactly one abstract method (SAM). default/static methods don't count, nor do abstract declarations of Object methods like equals.

@FunctionalInterface
interface Calculator { int apply(int a, int b); }

Calculator add = (a, b) -> a + b;         // lambda implements the SAM
Calculator max = Math::max;               // method reference
System.out.println(add.apply(3, 4));      // 7

@FunctionalInterface is optional but makes the compiler enforce the single-abstract-method rule. Built-ins you must know: Runnable (run), Comparator<T> (compare), Callable<V> (call), and the java.util.function family — Predicate<T> (test→boolean), Function<T,R> (apply), Consumer<T> (accept), Supplier<T> (get) — detailed in Lesson 14.

8.5 Marker (Tag) Interfaces

A marker interface is completely empty; implementing it tags a class with metadata the JVM or libraries check via instanceof:

  • Serializable — permits object serialization (Lesson 13); ObjectOutputStream throws NotSerializableException otherwise.
  • Cloneable — permits Object.clone(); without it you get CloneNotSupportedException.
  • RandomAccess — signals O(1) indexed access (ArrayList has it, LinkedList doesn't).

Modern alternative: annotations — but markers participate in the type system (void save(Serializable s)), which annotations cannot.

8.6 Interface vs Abstract Class — Quick Contrast

Interfaces: no state, no constructors, multiple inheritance, implicit public members, model a capability. Abstract classes: fields + constructors + any access level, single inheritance, model an is-a core with shared implementation. (Full table in Lesson 7.)

🎯 Exam Focus

  1. List the implicit modifiers of interface fields and methods. Why can't an interface have instance variables or constructors?
  2. How do interfaces provide multiple inheritance in Java while classes cannot? Explain the diamond problem and Java's three resolution rules with X.super.m().
  3. Why were default methods added in Java 8? Differentiate default and static interface methods.
  4. What is a functional interface? What does @FunctionalInterface enforce? Give four built-in examples.
  5. What is a marker interface? Name three from the JDK and explain how the JVM uses one of them.
  6. Write a program where class SmartPhone implements interfaces Camera and Phone that both declare a default spec() method, resolving the conflict explicitly.