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
interfacekeyword - Classes use
implementskeyword to implement interfaces - Can extend multiple interfaces using
extendskeyword
- Declared using the
-
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 interfaceInterfacePractice.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 Point | Abstract Class | Interface |
|---|---|---|
| Definition | Cannot 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 Method | Can have both implemented and abstract methods | Methods are abstract by default; Java 8 can have default and static methods |
| Inheritance | A class can inherit from only one abstract class | A class can implement multiple interfaces |
| Access Modifiers | Methods and properties can have any access modifier (public, protected, private) | Methods and properties are implicitly public |
| Variables | Can have member variables (final, non-final, static, non-static) | Variables are implicitly public, static, and final (constants) |
| Keyword Used | The abstract keyword is used to declare abstract class | The interface keyword is used to declare interface |
| Usage | when you have a group of closely related classes that share common functionality and fields | when you want unrelated classes to be able to perform a specific action, regardless of their inheritance hierarchy |
| Example | an Animal abstract class can have subclasses like Cat and Dog with shared fields like age and name | an AnimalStuff interface with a poop method can be implemented by both Animal subclasses and other classes that are not animals |