
What is Interface in Java:
Interface is a concept which is used to achieve complete abstraction in Java. There can be only abstract methods in the java interface not method body. We can’t create an object of the interface.
Interface defines the method signatures that classes need to implement, and classes that implement the interface provide their own implementations for these methods. Interfaces specify what a class must do and not how.
Reasons to Use Interfaces:
There are two main reasons to use interface in java:
1. To achieve 100% abstraction.
2. To achieve concept of multiple inheritance.
We can achieve fully abstraction by using the interface because it contains static constants and only abstract methods. A class that implementing an interface must implement all the methods of that interface
Syntax of an Interface:
To declare an interface, you use the interface keyword followed by the interface’s name. we can declare group of abstract methods inside interface.
interface InterfaceName {
void abstractMethod1();
void abstractMethod2();
}
Note: From Java 8, interface can have both default and static methods and from Java 9, it can have private methods as well here is the example below:
interface MyInterface {
// Regular abstract method (Java 8 and earlier)
void regularMethod();
// Default method (Java 8 and later)
default void defaultMethod() {
privateMethod(); // Calling private method
}
// Static method (Java 8 and later)
static void staticMethod() {
// Static method implementation
}
// Private method (Java 9 and later)
private void privateMethod() {
// Private method implementation
}
}
- We can not declare an interface as
private
orprotected
. Onlypublic
and default modifiers are allowed with interface.
private interface A { } // Compilation error
protected interface B { } // Compilation error
public interface C { } // Correct
interface D { } // Correct
- Every interface is implicitly considered abstract. This means that you don’t need to use the
abstract
keyword when declaring an interface.
interface MyInterface {} is equal to
abstract interface MyInterface{} // no need to write abstract here
- An interface can extend another interface using the
extends
keyword.
interface A { }
interface B extends A { } // Valid: B extends A
- Only classes can implement interfaces. An interface cannot implement another interface. This is because interfaces declare a contract of methods that classes must provide implementations
interface A { }
interface B implements A { } // Invalid: B cannot implement A
- Every variable of an interface is by default
public static
andfinal
while every method of an interface is by defaultpublic
andabstract
interface MyInterface {
void doSomething(); // public and abstract by default
// You can also declare constant fields, which are implicitly public, static, and final
int CONSTANT = 42; // public, static, and final by default
}
- When a class implements an interface in Java, the visibility (access modifier) of the methods being implemented must not be reduced. Reducing the visibility will result in compilation error
interface MyInterface {
void doSomething();
}
public class MyClass implements MyInterface {
@Override
public void doSomething() {
// Provide implementation
// private void doSomething() {
// This will give compilation error because the visibility is reduced public to private
}
}
- A class can implement any number of interfaces by the use of comma . This feature is known as “multiple interface inheritance.”
interface InterfaceA {
void methodA();
}
interface InterfaceB {
void methodB();
}
class MyClass implements InterfaceA, InterfaceB {
@Override
public void methodA() {
// Provide implementation
}
@Override
public void methodB() {
// Provide implementation
}
}
- Every variable of an interface implicitly
public
,static
, andfinal
. These variables are treated as constants and must be initialized with a value when they are declared. - They can be accessed using the interface name followed by the variable name.
interface Constants {
int VALUE = 42; // Implicitly public, static, and final
// Compilation error: Variables in interfaces must be initialized
// int otherValue;
}
class Main {
public static void main(String[] args) {
System.out.println(Constants.VALUE); // Accessing the constant using the interface name
// There's no need for a class to implement the interface to use its constants.
// Interface variables are treated as constants and can be accessed using the interface name itself
}
}
42
- All methods declared within an interface are implicitly
public
andabstract
. They don’t have method bodies (implementations) in the interface itself. - Classes implementing the interface must provide concrete implementations for these methods.
// Interface definition
interface MyInterface {
void doSomething(); // Implicitly public and abstract
}
// Class implementing the interface
class MyClass implements MyInterface {
@Override
public void doSomething() {
System.out.println("Doing something in MyClass");
}
}
public class Main {
public static void main(String[] args) {
MyInterface obj = new MyClass();
obj.doSomething(); // Calls the method from MyClass
}
}
Doing something in MyClass
- A class cannot implement two interfaces that declare a method with the same name but different return types. This would lead to ambiguity, as it would be unclear which method to call
interface A {
int run();
}
interface B {
void run();
}
class MyClass implements A, B {
// Not possible to define run() for both interfaces
//you could use different method names or rename them to avoid the conflict
}
compilation error
- If a class implements multiple interfaces and these interfaces have methods with the same signature, the implementing class should provide an implementation for only one of these methods.
interface InterfaceA {
void doSomething();
}
interface InterfaceB {
void doSomething();
}
class MyClass implements InterfaceA, InterfaceB {
@Override
public void doSomething() {
// This method implementation satisfies the contract for both interfaces
System.out.println("MyClass is doing something.");
}
}
public class Main {
public static void main(String[] args) {
MyClass myObject = new MyClass();
myObject.doSomething();
}
}
MyClass is doing something.
- If you attempt to call a method on the interface variable that is not declared in the interface, but is defined in the implementing class, it will result in a compilation error
interface MyInterface {
void display();
}
class MyClass implements MyInterface {
public void display() {
System.out.println("display method");
}
public void showMessage() {
System.out.println("showMessage method");
}
public static void main(String args[]) {
MyInterface obj = new MyClass(); // Assigning MyClass object to MyInterface type
// obj.showMessage(); // Error if uncommented, showMessage() is not an interface method
obj.display(); // prints "Inside display method"
}
}
display method
Summary of These Rules:
- A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces.
- Class needs to provide functionality for the all methods declared in the interface.
- Interface methods are implicitly
public
and abstract. You cannot declare them with thefinal
,static
, orprivate
modifiers. - An interface cannot be instantiated which means you can not create object of an interface.
- you can use an interface reference to interact with objects of the implementing classes:
- An interface can extend from one or many interfaces. Class can extend only one class but implement any number of interfaces
- An interface cannot implement another Interface. It has to extend another interface.
- interface variable must be initialized At the time of declaration, Otherwise, it will throw compilation error.
- The class cannot implement two interfaces in java that have methods with same name but different return type.
- Nested interfaces declared within another interface are implicitly
static
. You cannot declare them with thestatic
keyword. - Interfaces cannot have constructors because they cannot be instantiated directly.