-
Hajipur, Bihar, 844101
An interface in Java is a blueprint of a class that defines a set of abstract methods without providing their implementation. It specifies what a class must do, but not how it will do it. Interfaces are used to achieve complete abstraction and multiple inheritance in Java.
When a class implements an interface, it agrees to provide concrete definitions for all the methods declared inside that interface. In this way, interfaces help ensure a consistent structure across multiple classes that share common functionality but may implement it differently.
For example, an interface Vehicle might define methods like start() and stop(). Classes like Car and Bike can implement this interface and define these methods in their own ways.
Interfaces are mainly used to achieve abstraction, loose coupling, and multiple inheritance.
Abstraction: Interfaces hide implementation details and expose only the required methods.
Multiple Inheritance: Java does not support multiple inheritance using classes, but it allows a class to implement multiple interfaces.
Code Standardization: Interfaces define a contract that all implementing classes must follow.
Flexibility: Implementation can change without affecting interface definitions, which keeps the code modular.
For example, in a payment system, you can have an interface PaymentGateway with methods pay() and refund(). Different classes like UPIPayment, CardPayment, or WalletPayment can implement this interface and handle payments in their own way.
You can define an interface using the keyword interface, and implement it using the implements keyword in a class.
interface InterfaceName {
// method declarations
void method1();
void method2();
}
class ClassName implements InterfaceName {
public void method1() {
// method body
}
public void method2() {
// method body
}
}
interface Animal {
void makeSound();
void eat();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
public void eat() {
System.out.println("Dog eats bones");
}
}
class Cat implements Animal {
public void makeSound() {
System.out.println("Cat meows");
}
public void eat() {
System.out.println("Cat drinks milk");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.makeSound();
dog.eat();
Animal cat = new Cat();
cat.makeSound();
cat.eat();
}
}
Explanation:
Here, the interface Animal defines two abstract methods: makeSound() and eat(). The classes Dog and Cat implement this interface and define their own versions of these methods. This allows each class to behave differently while following the same structure.
From Java 8 onwards, interfaces can also contain default and static methods.
interface Vehicle {
void start();
default void fuelType() {
System.out.println("Most vehicles use petrol or diesel.");
}
static void serviceInfo() {
System.out.println("Vehicles should be serviced every 6 months.");
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car started successfully");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.fuelType();
Vehicle.serviceInfo();
}
}
Explanation:
The interface Vehicle defines a default method fuelType() and a static method serviceInfo(). Default methods provide a default implementation that can be inherited or overridden by implementing classes. Static methods can be called directly using the interface name.
Imagine you’re using a remote control. You press buttons like “volume up” or “channel change,” but you don’t need to know how the TV processes those actions. The remote defines a set of functions that every device must follow, regardless of brand or model.
Similarly, in Java, interfaces define a standard way of communication between classes. Different classes can implement the same interface and perform their own actions behind the scenes.
A major advantage of interfaces is that a class can implement multiple interfaces at once. This is useful because Java does not allow multiple inheritance with classes.
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class MultiFunctionPrinter implements Printable, Scannable {
public void print() {
System.out.println("Printing document...");
}
public void scan() {
System.out.println("Scanning document...");
}
}
public class Main {
public static void main(String[] args) {
MultiFunctionPrinter mfp = new MultiFunctionPrinter();
mfp.print();
mfp.scan();
}
}
Explanation:
The class MultiFunctionPrinter implements two interfaces, Printable and Scannable, and defines both methods. This is how Java supports multiple inheritance using interfaces.
| Feature | Interface | Abstract Class |
|---|---|---|
| Declaration | Declared using interface keyword |
Declared using abstract keyword |
| Methods | Can have abstract, default, and static methods | Can have abstract and non-abstract methods |
| Variables | All are public, static, and final by default | Can have instance and static variables |
| Inheritance | Supports multiple inheritance | Supports single inheritance |
| Implementation | Implemented using implements keyword |
Extended using extends keyword |
Achieves Full Abstraction: Only method declarations are provided, hiding implementation details.
Supports Multiple Inheritance: A class can implement multiple interfaces.
Ensures Consistency: All implementing classes must follow the same structure.
Improves Code Flexibility: Implementation can be changed without affecting the interface.
Promotes Loose Coupling: Reduces dependency between different parts of the program.
All interface methods are public and abstract by default.
Variables in an interface are public, static, and final.
Interfaces can extend other interfaces.
A class can implement multiple interfaces.
From Java 8 onward, interfaces can have default and static methods.
An interface in Java defines a set of rules that a class must follow. It is one of the key mechanisms for achieving abstraction and multiple inheritance. By using interfaces, you can write cleaner, flexible, and modular code that follows a consistent design. With the addition of default and static methods in Java 8, interfaces have become even more powerful and versatile in modern Java development.
Create an interface Animal with methods eat() and sleep(). Implement it in classes Dog and Cat to display their respective behaviors.
Define an interface Shape with a method calculateArea(). Implement it in classes Circle and Rectangle to calculate and display area based on user input.
Write a Java program that defines two interfaces Printable and Showable, both containing a method print(). Implement both interfaces in a class Document and show how to haShould I continue with the next tutorial — Java super Keyword?ndle the method conflict.
Create an interface BankAccount with methods deposit() and withdraw(). Implement it in SavingsAccount and CurrentAccount classes with suitable messages.
Design two interfaces InterfaceA and InterfaceB with one method each. Create a class TestClass that implements both interfaces and calls both methods.
Create an interface Vehicle with a method start(). Extend it with another interface ElectricVehicle that adds method chargeBattery(). Implement both in class TeslaCar and demonstrate usage.
Define an interface StudentOperations with methods addStudent() and viewStudent(). Implement it in class StudentDetails that performs basic operations on a list of students.
Write a Java program with an interface Playable that has methods Should I continue with the next tutorial — Java super Keyword?play() and pause(). Implement it in MusicPlayer and VideoPlayer classes to demonstrate polymorphism.
Create an interface RemoteControl with methods turnOn() and turnOff(). Implement it in classes TV and Fan to show different control behaviors.
Define an interface Calculator with methods for addition(), subtraction(), and multiplication(). Implement it in a class MathOperations and demonstrate usage in the main method.