interfaces in java
interfaces in java
method signatures, default methods, static methods, and nested types. Interfaces cannot have
instance variables or constructors. They are used to represent a contract or a blueprint that
other classes can implement.
1. Abstract Methods: An interface can contain methods that do not have a body. These
methods must be implemented by the class that implements the interface.
interface Animal {
void sound(); // Abstract method
}
2. Default Methods: Since Java 8, interfaces can also have default methods, which have
a body. These methods provide default behavior that classes can inherit, but they can
be overridden if needed.
interface Animal {
default void sleep() {
System.out.println("Animal is sleeping.");
}
}
3. Static Methods: Interfaces can also contain static methods that belong to the interface
itself, not to instances of implementing classes.
interface Animal {
static void move() {
System.out.println("Moving...");
}
}
4. Constants: All variables declared in an interface are implicitly public, static, and final
(i.e., constants). They must be initialized when declared.
interface Animal {
int MAX_AGE = 100; // Constant
}
Implementing an Interface:
A class that implements an interface must provide implementations for all of its abstract
methods, unless the class is abstract. A class can implement multiple interfaces, allowing for
multiple inheritances of behaviors (but not state).
interface Animal {
void sound();
}
interface Swimmer {
void swim();
}
Interfaces Declaration:
o Animal and Swimmer are interfaces, each declaring one method: sound() and
swim(), respectively.
Class Implementation:
o Dolphin is a class that implements both Animal and Swimmer interfaces.
o It provides concrete implementations for the sound() and swim() methods.
Usage:
In the Main class, we create an instance of Dolphin and call its sound() and swim() methods,
demonstrating polymorphism.
Decoupling Code: Interfaces allow classes to interact with each other without
knowing about their internal implementations.
Polymorphism: You can use objects of different classes in a similar way if they
implement the same interface.
Multiple Inheritance: Since Java doesn't support multiple inheritance with classes,
interfaces allow a class to inherit behavior from multiple sources.
Class vs Interface
The following table lists all the major differences between an interface and a
class in Java.
Features Class Interface
In Java, a class can implement more than one interface. This is one way Java supports
multiple inheritance, which is not directly supported by classes.
interface Animal {
void sound();
}
interface AnimalBehavior {
void sleep();
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
Default methods allow you to add functionality to interfaces without breaking the existing
implementations of classes. This is helpful when you want to add new methods to an
interface but don’t want to force all existing classes to implement those methods.
interface Vehicle {
void move();
// Default method
default void stop() {
System.out.println("Vehicle is stopping...");
}
}
In this example, the stop method is defined as a default method in the interface Vehicle, so the
Car class doesn't have to implement it. The Car class only implements the move method.
Interfaces can also contain static methods. These methods belong to the interface itself, not to
instances of implementing classes.
interface MathOperations {
static int add(int a, int b) {
return a + b;
}
One of the primary benefits of interfaces is that they allow you to create more flexible and
loosely-coupled code. You can use interfaces as types to allow different classes to interact
without being tightly bound to a particular implementation.
interface Payment {
void processPayment();
}
In this example, the Payment interface is used as a type to represent different payment
methods. This allows the client code (the Main class) to process payments without knowing
the specific implementation (whether it's CreditCardPayment or PayPalPayment).