Interfaces in Java Notes
Interfaces in Java Notes
Summary Notes
Introduction to Interfaces
• Defines what a class must do, not how it does
it.
• Supports abstraction and polymorphism.
• Declared using the 'interface' keyword.
• Similar to abstract methods but more
powerful.
Characteristics of Interfaces
• Method signatures only, no implementation.
• All implementing classes must define the
methods.
• A class can implement multiple interfaces.
• Interfaces can be reused across unrelated
classes.
Syntax of an Interface
• access interface InterfaceName {
• returnType method1(parameters);
• type CONSTANT1 = value;
• }
• Methods are implicitly public and abstract.
• Variables are public, static, and final.
Example: Series Interface
• public interface Series {
• int getNext();
• void reset();
• void setStart(int x);
• }
• Defines a number series interface.
Interface vs Abstract Class
• Interfaces: Only declarations, no constructors.
• Abstract Classes: Can have implementations,
constructors.
• Interfaces: Multiple inheritance supported.
• Abstract Classes: Single inheritance only.
Enhancements in JDK 8
• Introduced default methods in interfaces.
• Default methods can have implementations.
• Syntax: default void methodName() { ... }
• Allows backward-compatible interface
evolution.
Key Points to Remember
• Interfaces define a contract for behavior.
• Enable loose coupling and flexibility.
• Used for polymorphism and abstraction.
• Still relevant without default methods.