What is an interface? | Java Advanced | Part – 9

what is interface

Consider we have to write classes for cars which displays their individual features.

We will require different classes for different cars.

But there are a lot of features which are same across these cars.

We keep all these similar features in a single class, like a template and extend this class for specific cars.

Interfaces also work in a similar manner.

Interface are same as classes but in this we define only method names or variables. we define only what part of functions not how. The body of functions will be defined later according to requirement.

Interface acts as a reference for the other classes.

An interface is a collection of methods(without body) and constants.

These methods and constants can be used by classes implementing the interface.

Interfaces are used to achieve complete abstraction.

Also, interfaces can be used to achieve multiple inheritance in java, which is not possible otherwise.

An interface is written exactly like a class.

Let’s see how we can declare an interface.

Writing an Interface

To declare an interface, the keyword interface is used.

 public interface InterfaceName{

//Members

}

An interface is abstract, there is no need to use the abstract keyword to declare the interface.

Also, the methods declared inside the interface don’t need the keyword abstract. They are abstract by default.

And the methods in an interface are public by default.

Let’s begin by writing a simple interface.

public interface Student{

public void dispName();

public void dispRoll();

}

Here is an interface Student which contains two abstract methods dispName() and dispRoll().

Using an interface

To use the interface we have defined we need to implement it.

Yup, interfaces are implemented and not extended.

We use the keyword implements to do so.

A class can use more than one interface at a time.

Let’s see how we can implement an interface.

public class ClassName implements InterfaceName{

//Member definitions

}

The name of the class is followed by the keyword implements which is followed by the name of the interface.

Let’s implement the student interface that we defined previously.

public class Student1 implements Student {

public void dispName(){

//defining abstract method dispName

System.out.println(“Starlord”);

}

public void dispRoll(){

//defining abstract method dispRoll

System.out.println(“25”);

}

public static void main(String[] args) {

Student1 obj1 = new Student1();

obj1.dispName();

obj1.dispRoll();

}

}

Here, we have defined the abstract methods we declared in the interface Student.

Can you guess the output of the above snippet?

The output will be

Starlord 25

A class can implement an interface but just like a class can extend other classes, an interface can also extend other interfaces.

Let’s see how.

To do so, we use our very known keyword extends.

 public interface InterfaceName extends SomeOtherInterface{

//Members

}

So, if we want to extend the Student interface that we declared, we will write:

 public interface NewStudent extends Student{

public void admissionNumber();

}

Thus, NewStudent which has one method admissionNumber() inherits two other methods from the interface Student.

To Summarize

  • An interface is a collection of methods(abstract means without body ) and constants.
  • Interfaces are used to achieve complete abstraction and multiple inheritance.
  • An interface is abstract, there is no need to use the abstract keyword to declare the interface or the methods inside it.
  • A class can implement an interface but just like a class can extend other classes, an interface can also extend other interfaces.
  • A class can use more than one interface at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *