java interface with example

Java Interface with Example

In this tutorial, we’ll learn about interface in java with the help of examples, we will learn how to implement interface and when to use them in detail.

In Java, an interface is a mechanism for achieving abstraction. The Java interface can only contain abstract methods, no method bodies. In Java, it is used to achieve abstraction and multiple inheritance.

An interface is a class that is completely abstract. It consists of a collection of abstract methods.

In Java, the interface keyword is used to create an interface.

interface Sample {
  public void getdata();

  public void setdata();
}

Here,

  • Sample is an interface.
  • It includes abstract methods: setdata() and getdata().
  • In Java, an interface is a class blueprint. It includes static constants as well as abstract methods.
  • In other words, interfaces can include abstract methods and variables. It is not allowed to have a method body.
  • The IS-A relationship is also represented by the Java Interface.
  • It can’t be instantiated in the same way that the abstract class can.
  • We can have default and static methods in an interface since Java 8.
  • We can have private methods in an interface since Java 9.

Why use Java interface?

It’s a method to achieve complete abstraction. Because Java does not support multiple inheritance in the case of classes, multiple inheritance can be achieved by using interfaces. 

  • We can support multiple inheritance functionality via an interface.
  • It’s useful for achieving loose coupling.

Interface and classes

An object attributes and behaviors are described by its class. Class behaviors are defined by an interface. A class can have both abstract and concrete methods. An interface is made up entirely of abstract methods.

  • A class extends another class, an interface extends another interface, but a class implements an interface, as shown in the diagram below:
interface

Multiple inheritance in Java by interface

An interface, like a class, has variables and methods, but unlike a class, the methods in an interface are abstract by default. If a class implements multiple interfaces, or if an interface extends multiple interfaces, multiple inheritance by interface occurs.

Multiple inheritance in Java

Implementing an Interface

We can’t make interface objects the same way we can’t make abstract classes. Other classes must implement an interface in order to use it. To create an interface, we use the implements keyword.

interface Polygon {
  void getArea(int length, int breadth);
}

// implement the Polygon interface
class Rectangle implements Polygon {

  // implementation of abstract method
  public void getArea(int length, int breadth) {
    System.out.println("Area is: " + (length * breadth));
  }
}

class Main {
  public static void main(String[] args) {
    Rectangle r1 = new Rectangle();
    r1.getArea(10, 6);
  }
}

Output:

Area is: 60

  • Polygon is the name of the interface we created in the above example. getArea() is an abstract method in the interface ().
  • The Rectangle class implements Polygon in this case. Additionally, it implements the getArea() method.

Example: Java Interface

// create an interface
interface Language {
  void getName(String name);
}

// class implements interface
class Language1 implements Language {

  // implementation of abstract method
  public void getName(String lang) {
    System.out.println("Language is: " + lang);
  }
}

class Main {
  public static void main(String[] args) {
    Language1 language = new Language1();
    language.getName("Java Language");
  }
}

Output:

Language is: Java Language

  • In the preceding example, we created a Language interface. An abstract method called getName is included in the interface ().
  • The Language1 class implements the interface and implements the method in this case.

Example: A class in Java can implement multiple interfaces.

interface X {
  // members of X
}

interface Y {
  // members of Y
}

class Z implements X, Y {
  // abstract members of X
  // abstract members of Y
}

Extending an Interface

Interfaces, like classes, can extend other interfaces. Extending interfaces are done with the extends keyword. As an example:

interface Sample {
  // members of Sample interface
}

// extending interface
interface Basic extends Sample {
  // members of Sample interface
  // members of Basic interface
}

The Basic interface extends the Sample interface in this case. Now, any class that implements Basic must provide implementations for all of the Sample and Basic abstract methods.

Extending Multiple Interfaces

Multiple interfaces can be extended by a single interface.

interface X {
   ...
}
interface Y {
   ... 
}

interface Z extends X, Y {
   ...
}

Default methods in Java Interfaces

We can now add methods with implementation inside an interface with the release of Java 8. These are known as default methods.

The default keyword is used to declare default methods inside interfaces.

public default void getdata() {
   // body of getdata()
}

Why default methods used?

Let’s look at an example to see why default methods were introduced in Java.

  • Consider the situation where we need to add a new method to an interface.
  • We can easily add the method to our interface without having to implement it. However, the storey does not end there. All of our classes that implement that interface must include a method implementation.
  • If there are a lot of classes that implement this interface, we’ll have to keep track of them all and make changes to them. This is not only time-consuming, but also prone to errors.
  • Java introduced default methods to address this issue. Default methods are inherited in the same way that regular methods are.

Example: Default Method in Java Interface

interface Polygon {
  void getArea();

  // default method 
  default void getSides() {
    System.out.println("I can calculate the sides of a polygon..");
  }
}

// implements the interface
class Rectangle implements Polygon {
  public void getArea() {
    int length = 10;
    int breadth = 6;
    int area = length * breadth;
    System.out.println("Area is:" + area);
  }

  // overrides the getSides()
  public void getSides() {
    System.out.println("4 sides.");
  }
}

// implements the interface
class Square implements Polygon {
  public void getArea() {
    int length = 6;
    int area = length * length;
    System.out.println("Area is:" + area);
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of Rectangle
    Rectangle r = new Rectangle();
    r.getArea();
    r.getSides();

    // create an object of Square
    Square s = new Square();
    s.getArea();
    s.getSides();
  }
}

Output:

Area is: 30
4 sides.
Area is: 36
I can calculate the sides of a polygon.

  • Polygon is the name of the interface we created in the above example. It has a getSides() default method and an abstract getArea() method ().
  • We’ve created two classes that implement Polygon: Rectangle and Square.
  • The getArea() method is implemented by the Rectangle class, which also overrides the getSides() method. The Square class, on the other hand, only implements the getArea() method.
  • The overridden method is now called when the Rectangle object is used to call the getSides() method. The default method is called in the case of the Square object.

Private and static Methods in Interface

  • A new feature in Java 8 is the ability to include static methods within an interface.
  • We can use an interface’s references to access static methods, just like we can with a class. As an example,
// create an interface
interface Polygon {
staticMethod(){..}
}
// access static method
Polygon.staticMethod();

Note: Private methods are now supported in interfaces with the release of Java 9.
We are unable to create interface objects. As a result, private methods are used as helper methods

in interfaces, assisting other methods.

You may like:

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Hope this article will guide you to recognize all about the Java Interface with Example that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has One Comment

Leave a Reply