An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
It cannot be instantiated just like the abstract class.
The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members.
// Defining an interface
interface Animal {
void eat();
void sleep();
}
// Implementing the interface
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
// Main class to demonstrate usage
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat();
dog.sleep();
}
}
- Multiple inheritance in Java by interface
- Default Method in Interface
Starting from Java 8, interfaces can have default methods. Default methods are methods within an interface that have a default implementation. They allow you to add new methods to an existing interface without breaking compatibility with classes that already implement the interface.When a class implements an interface that contains a default method, it can choose whether to use the default implementation provided by the interface or provide its own implementation.interface Animal { void eat(); void sleep();
// Default method default void breathe() { System.out.println("Animal is breathing"); }}
class Dog implements Animal { @Override public void eat() { System.out.println("Dog is eating"); }
@Override public void sleep() { System.out.println("Dog is sleeping"); }}
public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.eat(); dog.sleep(); dog.breathe(); // Calling default method }}
interface Animal {
void eat();
void sleep();
// Default method
default void breathe() {
System.out.println("Animal is breathing");
}
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat();
dog.sleep();
dog.breathe(); // Calling default method
}
}
- Static Method in Interface
Starting from Java 8, interfaces can also contain static methods. Static methods in interfaces are similar to static methods in classes, but they are scoped within the interface and cannot be overridden in implementing classes.interface Animal { void eat(); void sleep();
// Static method static void info() { System.out.println("This is an Animal"); }}
class Dog implements Animal { @Override public void eat() { System.out.println("Dog is eating"); }
@Override public void sleep() { System.out.println("Dog is sleeping"); }}
public class Main { public static void main(String[] args) { Animal.info(); // Calling static method directly from interface
Dog dog = new Dog(); dog.eat(); dog.sleep(); }}
interface Animal {
void eat();
void sleep();
// Static method
static void info() {
System.out.println("This is an Animal");
}
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
public class Main {
public static void main(String[] args) {
Animal.info(); // Calling static method directly from interface
Dog dog = new Dog();
dog.eat();
dog.sleep();
}
}
- Private Method in Interface
Starting from Java 9, interfaces can contain private methods, which are methods scoped within the interface and are only accessible within the interface itself. Private methods in interfaces are primarily used to implement shared code or helper methods that are used by default or static methods within the interface.
interface Animal {
void eat();
void sleep();
// Default method
default void breathe() {
inhale();
exhale();
}
// Private method
private void inhale() {
System.out.println("Animal is inhaling");
}
// Private method
private void exhale() {
System.out.println("Animal is exhaling");
}
}
class Dog implements Animal {
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void sleep() {
System.out.println("Dog is sleeping");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat();
dog.sleep();
dog.breathe(); // Calling default method
}
}