Interface in Java

Subhadeep Ghosh
Javarevisited
Published in
3 min readMay 11, 2021

Interface is a very important feature of Java whose definition starts with the keyword interface. It is very much like a class with some differences. Basically through interface keyword you can declare and define an interface in java.

interface Interface_name

{

………………………………….

}

An interface basically specifies method declaration and contains fields or variables.

interface Interface_name

{

int a=5;

void method1();

}

N.B.: Methods of any interface are implicitly public and abstract and variables are public static and final.

How to implement an interface?

interface Ione

{

int a=5;

void method1();

}

class A implements Ione{

public void method1(){

System.out.println(a);

}

}

Classes in java basically implement interface through the keyword implements.

Now look at an example,

interface A {

void print();

}

class B implements A

{

public void print()

{

System.out.println(“It is warm up”);

}

}

public class Main {

public static void main(String[] args) {

A a;//interface reference

a=new B();//class B instance referred by interface reference a

a.print();

}

}

You can see in the above program, interface A contains a method print() which is defined and implemented by class B which implements interface A. As methods of an interface don’t have any implementation in that interface, they are implemented by class that implements that interface.

Now look at the main class very carefully. An interface creates a reference a but that reference refers to class B instance that implements that interface. Basically, the matter is, an interface cannot be instantiated. It creates reference and that reference refers the instance of any class which implements the interface.

Look at another example,

public interface myInterface {

int a = 10; // public, static and final

void display(); // public and abstract

}

class myClass implements myInterface {

public void display() // Implementing the abstract method

{

System.out.println(“Fine!”);

}

public static void main (String[] args) {

myClass m = new myClass ();

m.display();

System.out.println(“The final value a in myInterface “+ a);

}

}

Points to remember:

1. An interface cannot be instantiated.

2. Interfaces do not have constructors.

An interface can extend another interface:

interface I1 {

double x = 4.444;

void methodI1();

}

interface I2 extends I1 {

double y = 5.555;

void methodI2(); //public static by default

}

class A1 implements I2 {

public void methodI1(){

System.out.println(“From I1:”+(x+y));

}

public void methodI2(){

System.out.println(“From I2:”+(x+y));

}

}

public class myClass{

public static void main(String[] args) {

A1 a = new A1();

a.methodI1();

a.methodI2();

}

}

Here I2 interface extends the interface I1 and inherits its properties. So, when class A implements I2, it can use I1’s properties as well.

Multiple extension of interface:

package blog;

public interface IOne {

void print1();

}

public interface ITwo {

void print2();

}

public interface IThree extends IOne,ITwo{

void print3();

}

package blog;

public class Main implements IThree{

public void print1(){

System.out.println(“Method of IOne”);

}

public void print2(){

System.out.println(“Method of ITwo”);

}

public void print3(){

System.out.println(“Method of IThree”);

}

public static void main(String[] args) {

Main m1=new Main();

m1.print1();

m1.print2();

m1.print3();

}

}

Nested Interface:

An interface can be declared as a member of a class or another interface. Such an interface is called a nested interface. A nested interface can be declared as public, private, or protected.

package nestedinterface;

public class A {

public interface Nestin

{

boolean nonNegative(int x);

}

}

class B implements A.Nestin

{

public boolean nonNegative(int x)

{

return x<0 ? false:true;

}

}

package nestedinterface;

public class Main {

public static void main(String[] args) {

A.Nestin i1=new B();

System.out.println(i1.nonNegative(-1));

System.out.println(i1.nonNegative(5));

}

}

Related Topics:

Why java does not support multiple inheritance?

Inheritance in Java

--

--

Subhadeep Ghosh
Javarevisited

I am a student of MCA final year and really love programming.