Interface and Abstraction in Java - JDK 1.7, 1.8, 1.9 Features

Interface and Abstraction in Java - JDK 1.7, 1.8, 1.9 Features, Lambda & Functional Interface (2026)

Interface and Abstraction in Java - Complete Guide

Interface and Abstraction in Java

JDK 1.7 Features - Interface Rules

  1. Interface is a keyword used to achieve multiple inheritance.
  2. Interface is like a class used to hold constant variables and fully abstract methods.
  3. Constant variable can never change: public static final int i = 10; - public static final is implicit.
  4. Fully abstract method has no body: public abstract void eat(); - public abstract is implicit.
  5. You can never create an instance of an interface because it has no constructor.
  6. Interface always has public property (methods and variables are public by default).
  7. Interface can be used only through inheritance.
  8. A class can implement one or more interfaces separated by comma:
    class A implements B, C {} // B and C are Interfaces
  9. An interface can extend one or more interfaces:
    interface A extends B, C {} // B and C are interfaces
  10. A class can extend another class and also implement interfaces:
    class A extends B implements C, D {} // B is class, C,D are interfaces
  11. Interface can never be made final (because it must be inherited).
  12. Interface can be abstract (all interfaces are abstract by default).
  13. We can achieve loose coupling using interface.
  14. Interface can never extend or implement a class.
  15. A marker interface has no methods, e.g., Cloneable, Serializable.
  16. We can never use super keyword in interface (in JDK 1.7).
  17. All interface methods are implemented by its subclass using overriding.

JDK 1.8 Features

  1. In an interface you can define a non-static method using default keyword:
    default void show() { System.out.println("default method"); }
  2. We can also define static method inside an interface:
    static void display() {}
  3. We can use super keyword in interface using interface name:
    InterfaceName.super.methodName();

What is Functional Interface?

  1. An interface which has single abstract method only.
  2. Can have default and static methods also.
  3. Annotated with @FunctionalInterface

What is Lambda Expression?

  1. Lambda Expression is used to optimize your code (reduce boilerplate).
  2. Applies only with functional interface.
  3. Can be no-parameter or parameterized.
  4. Syntax: () -> System.out.println("No Parameterized");
  5. Syntax: (x) -> System.out.println("Parameterized: " + x);

JDK 1.9 Features

In an interface you can have private methods to reuse code inside default methods:

private void commonLogic() {
    // reusable code for default methods
}

Abstraction in Java

Abstraction means hiding complexity and showing only functionality to the user.

Using abstract class you can achieve abstraction. Let's see example:

Problem Without Abstraction - Complexity

Each and every Vehicle needs to provide implementation of all methods separately - lots of duplicate code:

interface Vehicle {
    void gear();
    void headLight();
    void backLight();
    void wheel();
    void seat();
}

class Bmw implements Vehicle {
    @Override public void gear() {}
    @Override public void headLight() {}
    @Override public void backLight() {}
    @Override public void wheel() {}
    @Override public void seat() {}
}

class Scoda implements Vehicle {
    @Override public void gear() {}
    @Override public void headLight() {}
    @Override public void backLight() {}
    @Override public void wheel() {}
    @Override public void seat() {}
}
// Same for Audi, Duster, Yamaha, Tvs, Suzuki, Scooty...
// Every class repeats same 5 methods - high complexity!
Problem: High complexity - each vehicle provides same implementations separately. Code duplication and hard to maintain.

Solution Using Abstraction - Clean Code

Using abstract classes we group common behavior - Car and Bike provide common implementation:

interface Vehicle {
    void gear();
    void headLight();
    void backLight();
    void wheel();
    void seat();
}

// Abstract class for all Cars - common implementation
abstract class Car implements Vehicle {
    @Override public void gear() { System.out.println("Car gear system"); }
    @Override public void headLight() {}
    @Override public void backLight() {}
    @Override public void wheel() {}
    @Override public void seat() {}
}

// Now Bmw, Scoda, Audi, Duster just extend Car - no need to override all methods
class Bmw extends Car {}
class Scoda extends Car {}
class Audi extends Car {}
class Duster extends Car {}

// Abstract class for all Bikes
abstract class Bike implements Vehicle {
    @Override public void gear() { System.out.println("Bike gear system"); }
    @Override public void headLight() {}
    @Override public void backLight() {}
    @Override public void wheel() {}
    @Override public void seat() {}
}

class Yamaha extends Bike {}
class Tvs extends Bike {}
class Suzuki extends Bike {}
class Scooty extends Bike {}

public class Test {
    public static void main(String[] args) {
        Vehicle v1 = new Bmw();
        Vehicle v2 = new Yamaha();
    }
}
Benefit: Using abstract class we achieve Abstraction - hides complexity, shows only functionality. Reduced code, easy to maintain, follows DRY principle.

Conclusion

Use interface to achieve 100% abstraction and multiple inheritance. Use abstract class to provide common implementation and achieve partial abstraction. From JDK 1.8, interfaces are more powerful with default and static methods.

No comments: