*Interface *
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods.
Key Properties of Interface:
- The interface in Java is a mechanism to achieve abstraction.
- By default, variables in an interface are public, static, and final.
- It is used to achieve abstraction and multiple inheritance in Java.
- It supports loose coupling (classes depend on behavior, not implementation).
- In other words, interfaces primarily define methods that other classes must implement.
- An interface in Java defines a set of behaviours that a class can implement, usually representing an IS-A relationship, but not always in every scenario.
import java.io.*;
// Interface Declared
interface testInterface {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// Class implementing interface
class TestClass implements testInterface {
// Implementing the capabilities of
// Interface
public void display(){
System.out.println("Geek");
}
}
class Geeks
{
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(t.a);
}
}
Output
Geek
10
Relationship Between Class and Interface
A class can extend another class, and similarly, an interface can extend another interface. However, only a class can implement an interface, and the reverse (an interface implementing a class) is not allowed.
When to Use Class and Interface?
** Use a Class when:**
- Use a class when you need to represent a real-world entity with attributes (fields) and behaviors (methods).
- Use a class when you need to create objects that hold state and perform actions
Classes are used for defining templates for objects with specific functionality and properties.
Use a Interface when:Use an interface when you need to define a contract for behavior that multiple classes can implement.
Interface is ideal for achieving abstraction and multiple inheritance.
Multiple Inheritance in Java Using Interface
Multiple Inheritance is an OOPs concept that can’t be implemented in Java using classes. But we can use multiple inheritances in Java using Interface. Let us check this with an example.
New Features Added in Interfaces in JDK 8
1. Default Methods
Example: This example demonstrates the use of default methods in interfaces (introduced in JDK 8) to provide a method implementation within the interface itself.
// interfaces can have methods from JDK 1.8 onwards
interface TestInterface
{
final int a = 10;
default void display() {
System.out.println("hello");
}
}
// A class that implements the interface.
class TestClass implements TestInterface
{
// Driver Code
public static void main (String[] args) {
TestClass t = new TestClass();
t.display();
}
}
output:
hello
2. Static Methods
Example: This example demonstrates the use of static methods in interfaces (introduced in JDK 8), which can be called directly using the interface name without needing an instance.
interface TestInterface
{
final int a = 10;
static void display()
{
System.out.println("hello");
}
}
// A class that implements the interface.
class TestClass implements TestInterface
{
// Driver Code
public static void main (String[] args)
{
TestInterface.display();
}
}
Output
hello
3. Private Methods
Example: This example demonstrates the use of private methods in interfaces (introduced in JDK 8) that can be called by default methods within the same interface but are not accessible outside the interface.
interface Vehicle {
// Private method for internal use
private void startEngine() {
System.out.println("Engine started.");
}
// Default method that uses the private method
default void drive() {
// Calls the private method
startEngine();
System.out.println("Vehicle is now driving.");
}
}
class Car implements Vehicle {
// Car class implements Vehicle interface and inherits the default method 'drive'
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
// This will call the default method, which in turn calls the private method
car.drive();
}
}
output:
Engine started.
Vehicle is now driving.
Extending Interfaces
Example: This example demonstrates how interface inheritance works in Java, where one interface (B) extends another (A), and a class (GFG) implements all the methods from both interfaces.
interface A {
void method1();
void method2();
}
// B now includes method1
// and method2
interface B extends A {
void method3();
}
// the class must implement
// all method of A and B.
class GFG implements B
{
public void method1() {
System.out.println("Method 1");
}
public void method2() {
System.out.println("Method 2");
}
public void method3() {
System.out.println("Method 3");
}
public static void main(String[] args){
// Instance of GFG class created
GFG x = new GFG();
// All Methods Called
x.method1();
x.method2();
x.method3();
}
}
output:
Method 1
Method 2
Method 3
Reference link:
https://www.geeksforgeeks.org/interfaces-in-java/