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.
Basics — Interfaces
Section: Java OOP. implements, multiple types, default/static/private methods, and functional interfaces.
A contract of behavior
interfacedeclares what a type can do. A classimplementsone or more interfaces and must implement the abstract methods.- Before Java 8, interfaces had only
public abstractmethods andpublic static finalconstants. Java 8 addeddefaultandstaticmethods; Java 9 addedprivatehelpers. - 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, Bis legal. IfAandBboth declaredefault void f(), C must overridef()and may callA.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).
@FunctionalInterfacedocuments that. Lambdas implement them (Runnable,Comparator,Function). defaultandstaticmethods do not count toward the SAM count.
| Feature | Since | Notes |
|---|---|---|
| Abstract methods | Java 1 | Implicitly public |
| Constants | Java 1 | public static final |
default methods | Java 8 | Instance body; can be overridden |
static methods | Java 8 | Called on the interface name |
private methods | Java 9 | Share code between defaults |
private static | Java 9 | Share code between statics/defaults |
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 ispublic static final. - A class can extend one class and implement many interfaces:
class Dog extends Animal implements Pet, Trainable.
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.
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());
}
} 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).
Runnableis one method for a reason. - Do not put state in interfaces; constants only.
@FunctionalInterfacemakes accidental second abstract methods a compile error.
Practice Programs List
- Define
Stackablewithpush/popand implement it with anArrayList. - Add a
default boolean isEmpty()that usessize(). - Create two interfaces with conflicting
default String name()and resolve them. - Write a
@FunctionalInterfaceand implement it with a lambda and with a method reference. - Implement
Comparableby score and sort an array withArrays.sort.