Interface
Interface
Interface
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();
}
A class can contain concrete (with The interface cannot contain concrete (with
implementation) methods implementation) methods.
The access specifiers used with classes are In Interface only one specifier is used-
private, protected, and public. Public.
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(t.a);
}
}
Output
Geek
10
import java.io.*;
interface Vehicle {
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){
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 {
Output
Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1
Example:
Java
// Java program to demonstrate How Diamond Problem
// Is Handled in case of Default Methods
// 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()
{
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
Display from Interface1
Print from Interface2
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.
Note: these methods are not inherited.
Java
// Java Program to show that interfaces can
// have methods from JDK 1.8 onwards
interface In1
{
final int a = 10;
static void display()
{
System.out.println("hello");
}
}
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");
}
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 implementation class with
an abstract modifier, and complete the remaining method implementation in the next
created child classes. It is possible to declare multiple child classes but at final we
have completed the implementation of all abstract methods.
In general, the development process is step by step:
Level 1 – interfaces: It contains the service details.
Level 2 – abstract classes: It contains partial implementation.
Level 3 – implementation classes: It contains all implementations.
Level 4 – Final Code / Main Method: It have access of all interfaces data.
Example:
Java
// Java Program for
// implementation Level wise
import java.io.*;
import java.lang.*;
import java.util.*;
// 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 {
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
Your deposit Amount :100
Your withdraw Amount :50