interface InterfaceName
{
// public static final variables (constants)
// public abstract methods
}
// Interface with 100% abstraction
interface Vehicle
{
void start();
void stop();
}
// Car class implements the interface
class Car implements Vehicle
{
public void start()
{
System.out.println("Car is starting...");
}
public void stop()
{
System.out.println("Car is stopping...");
}
}
// Main class to test
public class Main
{
public static void main(String[] args)
{
Vehicle v = new Car(); // Interface reference (polymorphism)
v.start();
v.stop();
}
}
Car is starting... Car is stopping...
// Interface with a common method
interface Printable
{
void print();
}
// Unrelated class 1
class Document implements Printable
{
public void print()
{
System.out.println("Printing document...");
}
}
// Unrelated class 2
class Image implements Printable
{
public void print()
{
System.out.println("Printing image...");
}
}
// Main class
public class Main
{
public static void main(String[] args)
{
Printable p1 = new Document();
Printable p2 = new Image();
p1.print();
p2.print();
}
}
Printing document... Printing image...
interface I1
{
void m1();
}
interface I2
{
void m2();
}
// Multiple Inheritance using interfaces
class A implements I1, I2
{
public void m1()
{
System.out.println("Method m1 from interface I1");
}
public void m2()
{
System.out.println("Method m2 from interface I2");
}
}
// Main class
public class Main
{
public static void main(String[] args)
{
A obj = new A();
obj.m1();
obj.m2();
}
}
Method m1 from interface I1 Method m2 from interface I2
import java.util.Scanner;
// Interface to achieve loose coupling
interface Payment
{
void pay();
}
// UPI Payment Implementation
class UpiPayment implements Payment
{
public void pay()
{
System.out.println("Payment done using UPI.");
}
}
// Net Banking Payment Implementation
class NetBankingPayment implements Payment
{
public void pay()
{
System.out.println("Payment done using Net Banking.");
}
}
// Checkout class using interface (not tightly bound to any one payment method)
class PaymentCheckout
{
void payment(Payment payment)
{
payment.pay(); // Loose coupling: works with any class that implements Payment
}
}
// Main class with switch-case
public class MainApp
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
PaymentCheckout checkout = new PaymentCheckout();
System.out.println("Choose payment method:");
System.out.println("1. UPI");
System.out.println("2. Net Banking");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
switch (choice)
{
case 1:
checkout.payment(new UpiPayment());
break;
case 2:
checkout.payment(new NetBankingPayment());
break;
default:
System.out.println("Invalid choice");
}
}
}
Choose payment method: 1. UPI 2. Net Banking Enter choice: 1 Payment done using UPI.If user enters 2:
Choose payment method: 1. UPI 2. Net Banking Enter choice: 2 Payment done using Net Banking.
CrudRepository, ApplicationContext.List, Set, Map.Connection, Statement.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.