Interfaces in Java - GeeksforGeeks
Interfaces in Java - GeeksforGeeks
Interfaces in Java - GeeksforGeeks
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception H
Interfaces in Java
Last Updated : 01 Apr, 2024
An Interface in Java programming language is defined as an abstract type
used to specify the behavior of a class. An interface in Java is a blueprint of a
behavior. A Java interface contains static constants and abstract methods.
When we decide on a type of entity by its behavior and not via attribute we
should define it as an interface.
https://www.geeksforgeeks.org/interfaces-in-java/ 1/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
AD
https://www.geeksforgeeks.org/interfaces-in-java/ 2/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
So, the question arises why use interfaces when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas
variables in the interface are final, public, and static.
// A simple interface
interface Player
{
final int id = 10;
int move();
}
Class Interface
https://www.geeksforgeeks.org/interfaces-in-java/ 3/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
Class Interface
A class can contain concrete (with The interface cannot contain concrete
implementation) methods (with implementation) methods.
The access specifiers used with classes In Interface only one specifier is used-
are private, protected, and public. Public.
Java
import java.io.*;
// A simple interface
interface In1 {
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
https://www.geeksforgeeks.org/interfaces-in-java/ 4/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
}
}
Output
Geek
10
Java
import java.io.*;
interface Vehicle {
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
https://www.geeksforgeeks.org/interfaces-in-java/ 5/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
// to increase speed
@Override
public void speedUp(int increment){
// to decrease speed
@Override
public void applyBrakes(int decrement){
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
// to decrease speed
@Override
public void applyBrakes(int decrement){
}
class GFG {
https://www.geeksforgeeks.org/interfaces-in-java/ 6/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
Output
https://www.geeksforgeeks.org/interfaces-in-java/ 7/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
Example:
Java
// Interface 1
interface API {
// Default method
default void show()
{
// Print statement
System.out.println("Default API");
}
}
// Interface 2
// Extending the above interface
interface Interface1 extends API {
// Abstract method
void display();
}
// Interface 3
// Extending the above interface
interface Interface2 extends API {
// Abstract method
void print();
}
// Main class
// Implementation class code
class TestClass implements Interface1, Interface2 {
// Overriding the abstract method from Interface1
public void display()
{
https://www.geeksforgeeks.org/interfaces-in-java/ 8/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
System.out.println("Display from Interface1");
}
// Overriding the abstract method from Interface2
public void print()
{
System.out.println("Print from Interface2");
}
// Main driver method
public static void main(String args[])
{
// Creating object of this class
// in main() method
TestClass d = new TestClass();
Output
Default API
1. Prior to JDK 8, the interface could not define the implementation. We can
now add default implementation for interface methods. This default
implementation has a special use and does not affect the intention behind
interfaces.
Java
https://www.geeksforgeeks.org/interfaces-in-java/ 9/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
interface In1
{
final int a = 10;
default void display()
{
System.out.println("hello");
}
}
Output
hello
2. Another feature that was added in JDK 8 is that we can now define static
methods in interfaces that can be called independently without an object.
Java
interface In1
{
final int a = 10;
static void display()
{
System.out.println("hello");
https://www.geeksforgeeks.org/interfaces-in-java/ 10/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
}
}
Output
hello
Extending Interfaces
One interface can inherit another by the use of keyword extends. When a class
implements an interface that inherits another interface, it must provide an
implementation for all methods required by the interface inheritance chain.
Program 1:
Java
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");
}
https://www.geeksforgeeks.org/interfaces-in-java/ 11/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
public void method3()
{
System.out.println("Method 3");
}
}
Program 2:
Java
interface Student
{
public void data();
}
class avi implements Student
{
public void data ()
{
String name="avinash";
int rollno=68;
System.out.println(name);
System.out.println(rollno);
}
}
public class inter_face
{
public static void main (String args [])
{
avi h= new avi();
h.data();
}
}
Output
avinash
68
In a Simple way, the interface contains multiple abstract methods, so write the
implementation in implementation classes. If the implementation is unable to
provide an implementation of all abstract methods, then declare the
https://www.geeksforgeeks.org/interfaces-in-java/ 12/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
Example:
Java
// Level 1
interface Bank {
void deposit();
void withdraw();
void loan();
void account();
}
// Level 2
abstract class Dev1 implements Bank {
public void deposit()
{
System.out.println("Your deposit Amount :" + 100);
}
}
// Level 3
class Dev3 extends Dev2 {
https://www.geeksforgeeks.org/interfaces-in-java/ 13/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
public void loan() {}
public void account() {}
}
// Level 4
class GFG {
public static void main(String[] args)
{
Dev3 d = new Dev3();
d.account();
d.loan();
d.deposit();
d.withdraw();
}
}
Output
1. Static methods
2. Private methods
3. Private Static methods
https://www.geeksforgeeks.org/interfaces-in-java/ 14/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
A class that implements the interface must implement all the methods in
the interface.
All the methods are public and abstract. And all the fields are public, static,
and final.
It is used to achieve multiple inheritances.
It is used to achieve loose coupling.
Inside the Interface not possible to declare instance variables because by
default variables are public static final.
Inside the Interface, constructors are not allowed.
Inside the interface main method is not allowed.
Inside the interface, static, final, and private methods declaration are not
possible.
Must Read
Access Specifier of Methods in Interfaces
Access Specifiers for Classes or Interfaces in Java
Abstract Classes in Java
Comparator Interface in Java
Java Interface Methods
Nested Interface in Java
1. Functional Interface
https://www.geeksforgeeks.org/interfaces-in-java/ 15/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
2. Marker interface
Feeling lost in the vast world of Backend Development? It's time for a change!
Join our Java Backend Development - Live Course and embark on an exciting
journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Previous Next
Similar Reads
Callback using Interfaces in Java Private Methods in Java 9 Interfaces
https://www.geeksforgeeks.org/interfaces-in-java/ 16/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
Company Explore
About Us Hack-A-Thons
Legal GfG Weekly Contest
Careers DSA in JAVA/C++
https://www.geeksforgeeks.org/interfaces-in-java/ 17/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive
https://www.geeksforgeeks.org/interfaces-in-java/ 19/20
4/25/24, 6:25 PM Interfaces in Java - GeeksforGeeks
https://www.geeksforgeeks.org/interfaces-in-java/ 20/20