Interfaces in Java: Defining, Implementing, Variables & Extending Interfaces
1. What is an Interface in Java?
An Interface in Java is a blueprint of a class that acts as a 100% pure contract specifying what a class must do, but not how it does it.
It is a collection of abstract methods (methods without a body) and constant variables.
Real-Life Analogy: Think of a 3-Pin Electric Socket on your wall as an
Interface. The socket defines a strict contract (3 pins, 230V AC), without caring whether you plug in a Laptop Charger, a Television, or an Iron! Any appliance that implements that 3-pin plug contract can use the socket.
Two Major Reasons Why Interfaces Exist in Java:
- To achieve Full (100%) Abstraction and loose coupling.
- To support Multiple Inheritance in Java safely (a single class can implement multiple interfaces at the same time!).
2. Defining an Interface & Variables in Interfaces
An interface is declared using the interface keyword instead of class.
Automatic Compiler Rules Inside an Interface:
- Variables in Interfaces: Every variable declared inside an interface is implicitly
public static final(a global constant!). You must initialize it at the time of declaration, and its value can never be changed by any implementing class. - Methods in Interfaces: Every classic method declared inside an interface is implicitly
publicandabstract(no method body). - Like an abstract class, an interface cannot have constructors and cannot be instantiated directly (
new Printable()is invalid).
Syntax for Defining an Interface
interface PaymentGateway {
// 1. Variable in Interface -> automatically: public static final
String CURRENCY = "INR";
int MAX_LIMIT = 100000;
// 2. Method in Interface -> automatically: public abstract
void processPayment(double amount);
}
3. Implementing and Applying Interfaces
To use an interface, a class uses the implements keyword (instead of extends) and must provide concrete bodies for all abstract methods declared in the interface.
- Important Rule: Because interface methods are implicitly
public, when a class implements an interface method, it MUST declare the method aspublic!
class UPIPayment implements PaymentGateway {
@Override
public void processPayment(double amount) {
System.out.println("Paid " + CURRENCY + " " + amount + " successfully via UPI!");
}
}
4. Achieving Multiple Inheritance Using Interfaces
In Java, a class cannot extend more than one class (class C extends A, B is illegal), but a single class CAN implement multiple interfaces separated by a comma:
class SmartPhone implements Camera, MusicPlayer { ... }
print()) + interface Showable (show())
Safely inherits from BOTH interfaces without the Diamond Problem!
Complete Program: Multiple Inheritance via Interfaces
interface Printable {
void print();
}
interface Showable {
void show();
}
// One class implementing TWO interfaces simultaneously (Multiple Inheritance)
class ExamReport implements Printable, Showable {
@Override
public void print() {
System.out.println("Printing BCA Semester Marksheet on paper...");
}
@Override
public void show() {
System.out.println("Displaying BCA Marksheet on screen...");
}
}
public class MultipleInheritanceDemo {
public static void main(String[] args) {
ExamReport report = new ExamReport();
report.print();
report.show();
}
}
5. Extending Interfaces (Interface Inheritance)
One interface can inherit from another interface using the extends keyword! When a class implements the child interface, it must provide implementations for the methods of both the child interface and the parent interface.
Golden Keyword Rules to Remember (extends vs implements):
- Class → Class: uses
extends(class B extends A) - Class → Interface: uses
implements(class B implements I1, I2) - Interface → Interface: uses
extends(interface I2 extends I1)
interface BasicCalculator {
void add(int a, int b);
}
// An interface EXTENDING another interface!
interface ScientificCalculator extends BasicCalculator {
void power(double base, double exp);
}
// Class implementing child interface must implement BOTH methods
class MyCalc implements ScientificCalculator {
public void add(int a, int b) {
System.out.println("Sum = " + (a + b));
}
public void power(double base, double exp) {
System.out.println("Power = " + Math.pow(base, exp));
}
}
6. Comparison: Abstract Class vs Interface
This is one of the most important 8-mark comparison questions in Unit 2 of DAVV BCA exams!
| Feature | Abstract Class | Interface |
|---|---|---|
| 1. Keyword Used | Declared using abstract class. Inherited using extends. |
Declared using interface. Implemented using implements. |
| 2. Multiple Inheritance | A subclass can extend only ONE abstract class. | A class can implement MULTIPLE interfaces (implements I1, I2). |
| 3. Types of Methods | Can have both abstract methods and normal concrete methods with bodies. | Traditionally contains only abstract methods (implicitly public abstract). |
| 4. Variables (Fields) | Can have private, protected, non-final, and non-static instance variables. |
All variables are strictly public static final constants. |
| 5. Constructors | Can have constructors (called via super() when a subclass is instantiated). |
Cannot have constructors at all. |
| 6. Access Modifiers | Methods and variables can be public, protected, private, or default. |
Members are public by default. |
💡 Exam Preparation Tips for DAVV BCA
- If asked “How are variables treated inside a Java interface?”, always write the three keywords:
public static final. - If asked “How does Java support Multiple Inheritance?”, explain why multiple inheritance of classes fails (Diamond Problem) and write the
ExamReport implements Printable, Showableprogram above!