Interfaces in Java
An interface in Java defines a contract that other classes can implement. It declares a set of methods that implementing classes must provide but doesn’t contain any implementation details itself; it only defines the method signatures.
It is commonly used to achieve multiple inheritance, as classes in Java cannot inherit from more than one class but can implement multiple interfaces. It is also used to achieve full abstraction, as interfaces contain no method bodies.
Basic Structure :
public interface MyInterface {
// Declare methods (no implementation)
void methodName();
}
Interface Declaration:
Interfaces are declared using the interface keyword followed by the interface name (e.g., MyInterface in this example).
By convention, interface names in Java typically start with an uppercase letter and are descriptive of the behavior they define.
Scenario: Payment Processing System
Suppose you are developing a payment processing system for an e-commerce platform. The system must support different payment methods, such as Credit Card, PayPal, and Bank Transfer. In the future, new payment methods, such as Bitcoin or Apple Pay, may be added. To make the system flexible and extendable, you can use an interface that defines the contract for any payment method.
Example
// Define the IPayment interface for different payment methods
public interface IPayment {
void processPayment(double amount); // Contract for processing payments
}
// Define the ILogging interface for logging payment details
public interface ILogging {
void logPayment(String message); // Contract for logging payment actions
}
// CreditCard class that implements both IPayment and ILogging interfaces
public class CreditCard implements IPayment, ILogging {
@Override
public void processPayment(double amount) {
System.out.println(“Processing Credit Card payment of ” + amount + ” dollars.”);
}
@Override
public void logPayment(String message) {
System.out.println(“CreditCard Log: ” + message);
}
}
// PayPal class that implements both IPayment and ILogging interfaces
public class PayPal implements IPayment, ILogging {
@Override
public void processPayment(double amount) {
System.out.println(“Processing PayPal payment of ” + amount + ” dollars.”);
}
@Override
public void logPayment(String message) {
System.out.println(“PayPal Log: ” + message);
}
}
// Main program to demonstrate payment and logging with multiple interfaces
public class Main {
public static void main(String[] args) {
// Process and log payment using Credit Card
CreditCard creditCardPayment = new CreditCard();
creditCardPayment.processPayment(150.00); // Processing Credit Card payment
creditCardPayment.logPayment(“Credit Card payment of 150 dollars was successful.”); // Logging
// Process and log payment using PayPal
PayPal payPalPayment = new PayPal();
payPalPayment.processPayment(200.50); // Processing PayPal payment
payPalPayment.logPayment(“PayPal payment of 200.50 dollars was successful.”); // Logging
}
}
Explanation:
Define the IPayment interface for different payment methods
public interface IPayment {
void processPayment(double amount); // Contract for processing payments
}
The IPayment interface defines a contract for payment processing methods.
It declares the processPayment method, which must be implemented by any class using this interface.
Define the ILogging Interface for Logging Payment Details
public interface ILogging {
void logPayment(String message); // Contract for logging payment actions
}
The ILogging interface defines a contract for logging.
It declares the logPayment method, ensuring that implementing classes provide a way to log payment-related messages.
CreditCard Class that Implements Both IPayment and ILogging Interfaces
public class CreditCard implements IPayment, ILogging {
@Override
public void processPayment(double amount) {
System.out.println(“Processing Credit Card payment of ” + amount + ” dollars.”);
}
@Override
public void logPayment(String message) {
System.out.println(“CreditCard Log: ” + message);
}
}
The CreditCard class implements both IPayment and ILogging.
It provides specific behavior for processing a credit card payment and logging the action.
PayPal Class that Implements Both IPayment and ILogging Interfaces
public class PayPal implements IPayment, ILogging {
@Override
public void processPayment(double amount) {
System.out.println(“Processing PayPal payment of ” + amount + ” dollars.”);
}
@Override
public void logPayment(String message) {
System.out.println(“PayPal Log: ” + message);
}
}
The PayPal class also implements both IPayment and ILogging.
It provides specific implementations for processing a PayPal payment and logging the action.
Main Program to Demonstrate Payment and Logging with Multiple Interfaces
public class Main {
public static void main(String[] args) {
// Process and log payment using Credit Card
CreditCard creditCardPayment = new CreditCard();
creditCardPayment.processPayment(150.00); // Processing Credit Card payment
creditCardPayment.logPayment(“Credit Card payment of 150 dollars was successful.”); // Logging
// Process and log payment using PayPal
PayPal payPalPayment = new PayPal();
payPalPayment.processPayment(200.50); // Processing PayPal payment
payPalPayment.logPayment(“PayPal payment of 200.50 dollars was successful.”); // Logging
}
}
Task:
1. Basic Interface Implementation:
Create an interface IAnimal with a method speak(). Implement this interface in two classes Dog and Cat with their respective implementations of the speak() method. In the main method, create instances of Dog and Cat and call their speak() methods.
2. Interface with Properties and Methods:
Define an interface IVehicle with a property speed and a method drive(). Implement this interface in two classes Car and Bike. Set the speed and implement the drive() method to print a message indicating the vehicle’s speed. In the main method, create instances of Car and Bike, set their speeds, and call the drive() method.
3. Interface Inheritance:
Create an interface IShape with methods calculateArea() and calculatePerimeter(). Create another interface IColoredShape that inherits from IShape and adds a property color. Implement IColoredShape in a class Rectangle. In the main method, create an instance of Rectangle, set its properties, and call its methods.
4. Interface and Abstract Class Combination:
Create an interface IRunnable with a method run(), and an abstract class Machine with an abstract method start(). Create a class Robot that inherits from Machine and implements IRunnable. Provide implementations for the start() and run() methods. In the main method, create an instance of Robot and call its methods.
The training covers Java interfaces, including their concepts, syntax, and practical applications.
Yes, it includes examples demonstrating how to define interfaces and implement them in classes.
Yes, exercises involve creating interfaces and implementing them in various classes to understand their usage in different scenarios.
Yes, it’s designed for beginners with clear explanations and step-by-step examples.
A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler will suffice.
Yes, it covers how interfaces provide a way to achieve abstraction and multiple inheritance in Java.
Yes, the training is self-paced and accessible online anytime.
Yes, it explains the distinctions and appropriate use cases for interfaces and abstract classes.
It typically takes a few hours to complete, depending on your learning pace.
Yes, this Java Interface training is completely free on Iqra Technology Academy’s website.