Skip to main content

Java OOP

Interfaces

Complete lesson for Interfaces using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Interfaces

Section: Java OOP. implements, multiple types, default/static/private methods, and functional interfaces.

A contract of behavior

  • interface declares what a type can do. A class implements one or more interfaces and must implement the abstract methods.
  • Before Java 8, interfaces had only public abstract methods and public static final constants. Java 8 added default and static methods; Java 9 added private helpers.
  • A variable can have an interface type: List xs = new ArrayList<>();.
interface Printer {
    void print(String msg);

    default void printLine(String msg) {
        print(msg + System.lineSeparator());
    }

    static Printer stdout() {
        return System.out::print;
    }
}

public class InterfaceDemo implements Printer {
    @Override
    public void print(String msg) {
        System.out.print("[out] " + msg);
    }

    public static void main(String[] args) {
        Printer p = new InterfaceDemo();
        p.printLine("hello");
        Printer.stdout().print("via factory\n");
    }
}

Multiple interfaces and default conflicts

  • class C implements A, B is legal. If A and B both declare default void f(), C must override f() and may call A.super.f().
  • A class method always wins over a default method with the same signature (class wins).

Functional interfaces

  • A functional interface has exactly one abstract method (SAM). @FunctionalInterface documents that. Lambdas implement them (Runnable, Comparator, Function).
  • default and static methods do not count toward the SAM count.
FeatureSinceNotes
Abstract methodsJava 1Implicitly public
ConstantsJava 1public static final
default methodsJava 8Instance body; can be overridden
static methodsJava 8Called on the interface name
private methodsJava 9Share code between defaults
private staticJava 9Share code between statics/defaults
L2

Advanced — Interfaces

Evolution and marker interfaces

  • Default methods let library authors add methods without breaking every implementor (List.sort).
  • Marker interfaces (Serializable, RandomAccess) have no methods; they tag a type for the runtime or algorithms.
  • Interface fields are constants. int MAX = 10; in an interface is public static final.
  • A class can extend one class and implement many interfaces: class Dog extends Animal implements Pet, Trainable.
L3

Tricky Interview Points

Q1) Can an interface have a constructor?

Answer No. There is no instance to construct until a class (or lambda) implements it.

Q2) Are interface methods public?

Answer Abstract and default methods are public (you may write public). Java 9 private methods are the exception.

Q3) What is a default method?

Answer A method with a body in an interface, introduced in Java 8 so APIs can evolve and to support mixin-style behavior.

Q4) What happens if two interfaces have the same default method?

Answer The implementing class must override it. It can delegate with InterfaceName.super.method().

Q5) What is a functional interface?

Answer An interface with a single abstract method. Lambdas and method references need one.

Q6) Interface vs abstract class — one line?

Answer Interface = capability you can mix in. Abstract class = shared lineage and state.

Q7) Can you instantiate an interface?

Answer Not directly. You can write an anonymous class or a lambda if it is functional.

L4

Simple Programs

Two interfaces, one class

interface Camera {
    default String kind() {
        return "camera";
    }
}

interface Gps {
    default String kind() {
        return "gps";
    }
}

public class Phone implements Camera, Gps {
    @Override
    public String kind() {
        return Camera.super.kind() + "+" + Gps.super.kind();
    }

    public static void main(String[] args) {
        System.out.println(new Phone().kind());
    }
}
L5

Problem Solving Tricks

  • Program to the interface: declare List, Map, Set — not the concrete class — unless you need a concrete API.
  • Keep interfaces small (ISP). Runnable is one method for a reason.
  • Do not put state in interfaces; constants only.
  • @FunctionalInterface makes accidental second abstract methods a compile error.
L6

Practice Programs List

  1. Define Stackable with push/pop and implement it with an ArrayList.
  2. Add a default boolean isEmpty() that uses size().
  3. Create two interfaces with conflicting default String name() and resolve them.
  4. Write a @FunctionalInterface and implement it with a lambda and with a method reference.
  5. Implement Comparable by score and sort an array with Arrays.sort.