Skip to Content
bytegarden

Java Interfaces

  • Definition: An interface is a reference type in Java that defines a contract of abstract behavior that classes must implement

  • Key Characteristics:

    • Contains primarily abstract methods (no implementation details)
    • Provides pure abstraction
    • Enables polymorphism and loose coupling
    • Similar to an abstract class but with stricter constraints
  • Syntax:

    • Declared using the interface keyword
    • Classes use implements keyword to implement interfaces
    • Can extend multiple interfaces using extends keyword
  • Members:

    • Methods are implicitly public abstract
    • Properties are implicitly public static final
    • Can include default methods (with implementation) since Java 8
    • Can include private methods since Java 9
  • Multiple Implementation:

    • A class can implement multiple interfaces
    • Provides alternative to multiple inheritance
    • Helps achieve polymorphic behavior
  • Properties of fields/variables in Interface:

    • Must be constants (public static final)
    • Must be initialized at declaration
    • Cannot be modified after initialization
  • Static Methods:

    • Loaded into memory at class loading time
    • Stored in Method Area
    • Can be called without object instantiation
    • Cannot be overridden by implementing classes
  • See this example (InterfacePractice.java, below) of interface

    InterfacePractice.java
    // program on interfaces using `interface` keyword and `implements` keyword
    interface TestInterface {
        int X = 10; // implicitly public static final
    
        void meth1(); // implicitly public abstract
    
        void meth2();
    
        static void meth3() {
            System.out.println("Meth3 of TestInterface");
        }
    }
    
    interface Test2Interface extends TestInterface {
        void meth4();
    
        static void meth5() {
            System.out.println("Meth5 of Test2Interface");
        }
    }
    
    class MyClass implements Test2Interface {
        
        @Override
        public void meth1() {
            System.out.println("Meth1");
        }
    
        @Override
        public void meth2() {
            System.out.println("Meth2");
        }
    
        @Override
        public void meth4() {
            System.out.println("Meth4");
        }
    }
    
    public class InterfacePractice {
        public static void main(String[] args) {
            System.out.println(TestInterface.X);
            // TestInterface.X='213'; // error: cannot assign a value to final variable X
            TestInterface.meth3();
            
    
            Test2Interface.meth5();  // prints the method 5 in Test2Interface as
    
            MyClass obj = new MyClass();
            obj.meth1();
            obj.meth2();
            obj.meth4();
        }
    }
    Runs in your browser with Java 21. Ctrl/โŒ˜ + Enter to run.Built with CheerpJ

Abstract Class v/s Interface

Comparison PointAbstract ClassInterface
DefinitionCannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation)Specifies a set of methods a class must implement; methods are abstract by default
Implementation MethodCan have both implemented and abstract methodsMethods are abstract by default; Java 8 can have default and static methods
InheritanceA class can inherit from only one abstract classA class can implement multiple interfaces
Access ModifiersMethods and properties can have any access modifier (public, protected, private)Methods and properties are implicitly public
VariablesCan have member variables (final, non-final, static, non-static)Variables are implicitly public, static, and final (constants)
Keyword UsedThe abstract keyword is used to declare abstract classThe interface keyword is used to declare interface
Usagewhen you have a group of closely related classes that share common functionality and fieldswhen you want unrelated classes to be able to perform a specific action, regardless of their inheritance hierarchy
Examplean Animal abstract class can have subclasses like Cat and Dog with shared fields like age and namean AnimalStuff interface with a poop method can be implemented by both Animal subclasses and other classes that are not animals