What is Inheritance ?
- An interface is a completely abstract blueprint that defines what a class must do but not how it does it.
- It contains abstract methods that implementing classes must provide.
- It achieves 100% abstraction and supports multiple inheritance in Java.
Example :
interface Animal {
void sound(); // automatically public abstract
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
By default methods are :
public and abstract.
By default variables are :
public static final.
Can you create constructor in interface ?
No, because interfaces cannot be instantiated and do not have instance variables to initialize.
Can you create object for interface ?
No, because it is incomplete and contains abstract methods without implementation. An object requires complete method definitions, which are provided by implementing classes.
Default methods in interface:
- It is a method inside an interface that has a body (implementation).
- Before Java 8 → Interface could only have abstract methods.
- After Java 8 → Interfaces can have default and static methods.
Why Default Methods Were Introduced?
- If a new method is added as abstract in interface, Classes that implements the interface should add Definition to it, otherwise compile time error will come. But default methods allow adding new methods to interfaces without breaking existing implementations.
// Before Java 8 — if you add a new method to interface
interface Vehicle {
void start();
void stop();
void fly(); // NEW method added — ALL implementing classes BREAK
}
// Every class that implements Vehicle must now implement fly()
// even if they don't need it — this is a problem!
class Car implements Vehicle { } // must implement fly()
class Bike implements Vehicle { } // must implement fly()
class Bus implements Vehicle { } // must implement fly()
// Java 8+ — add default method — NO existing classes break
interface Vehicle {
void start();
void stop();
// default method — optional to override
default void fly() {
System.out.println("This vehicle cannot fly");
}
}
class Car implements Vehicle {
public void start() { System.out.println("Car started"); }
public void stop() { System.out.println("Car stopped"); }
// fly() is optional — inherited from interface
}
What happens when a class implements two interfaces with the same default method?
- conflict occurs. If two interfaces have same default method — implementing class must override it to solve the conflict.
public interface Mom {
default void work(){
System.out.println("Mom is working");
}
}
public interface Dad {
default void work(){
System.out.println("Dad is working");
}
}
public class Son implements Mom,Dad{
public void work() {
Mom.super.work();
Dad.super.work();
}
public static void main(String[] args) {
B ob = new B();
ob.work();
}
}
Output :
Mom is working
Dad is working
What is Static Methods in Interface?
- Static methods in interfaces were introduced in Java 8. They are similar to static methods in classes — they belong to the interface itself, not to any implementing class or object.
- They must be called using the interface name
- They cannot be overridden
- They can call other static methods within the same interface
- They cannot call abstract or default methods of the interface
interface UserValidator {
// abstract method
boolean validate(String value);
// static utility methods
static boolean isUsernameValid(String username) {
if (username == null || username.isEmpty()) return false;
return username.length() >= 3 &&
username.length() <= 20 &&
username.matches("[a-zA-Z0-9_]+");
}
static boolean isPasswordValid(String password) {
if (password == null || password.isEmpty()) return false;
return password.length() >= 8 &&
password.matches(".*[A-Z].*") && // has uppercase
password.matches(".*[0-9].*") && // has number
password.matches(".*[!@#$%].*"); // has special char
}
static boolean isEmailValid(String email) {
if (email == null || email.isEmpty()) return false;
return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
}
static boolean isPhoneValid(String phone) {
if (phone == null || phone.isEmpty()) return false;
return phone.matches("\\d{10}");
}
}
// Main
public class Main {
public static void main(String[] args) {
System.out.println(UserValidator.isUsernameValid("john_doe")); // true
System.out.println(UserValidator.isUsernameValid("jo")); // false
System.out.println(UserValidator.isPasswordValid("Pass@1234")); // true
System.out.println(UserValidator.isPasswordValid("password")); // false
System.out.println(UserValidator.isEmailValid("john@gmail.com")); // true
System.out.println(UserValidator.isEmailValid("johngmail.com")); // false
System.out.println(UserValidator.isPhoneValid("9876543210")); // true
System.out.println(UserValidator.isPhoneValid("98765")); // false
}
}
An interface can extend one or more interfaces :
interface Payment {
void pay(double amount);
}
interface Refundable {
void refund(double amount);
}
interface OnlinePayment extends Payment, Refundable {
void generateReceipt();
}
class UpiPayment implements OnlinePayment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using UPI");
}
@Override
public void refund(double amount) {
System.out.println("Refunded " + amount);
}
@Override
public void generateReceipt() {
System.out.println("Receipt generated");
}
}
public class Main {
public static void main(String[] args) {
OnlinePayment payment = new UpiPayment();
payment.pay(1000);
payment.refund(200);
payment.generateReceipt();
}
}
private methods allowed from Java 9 :
- Private methods were introduced in Java 9 to allow code reuse inside interfaces, mainly to support default and static methods without duplicating logic.
Rules for Private Methods in Interface :
- Must have body.
- Cannot be abstract.
- Cannot be accessed outside interface.
- Can be called only by : Default methods Static methods inside same interface.
Problem Before Java 9 :
interface Logger {
default void logInfo(String msg) {
System.out.println("INFO: " + msg);
System.out.println("Logged successfully");
}
default void logError(String msg) {
System.out.println("ERROR: " + msg);
System.out.println("Logged successfully");
}
}
System.out.println("Logged successfully"); is repeated.
Solution in Java 9 :
interface Logger {
default void logInfo(String msg) {
printMessage("INFO: " + msg);
}
default void logError(String msg) {
printMessage("ERROR: " + msg);
}
// Private method
private void printMessage(String message) {
System.out.println(message);
System.out.println("Logged successfully");
}
}
Key Rules to Remember :
- Interface methods are public abstract by default.
- Interface variables are public static final by default.
- A class must implement all abstract methods or be declared abstract.
- Interface cannot have constructor.
- Interface cannot be instantiated.
- A class can implement multiple interfaces.
- An interface can extend one or more interfaces, but it cannot implement an interface. Implementation is done only by classes.
- default and static methods allowed from Java 8.
- private methods allowed from Java 9.
Top comments (0)