Interface in Java – Latest Features of JDK 8 & JDK 9

1. Objective

In our last tutorial, we discussed Collections in Java. Now, in this tutorial for Java we are going to learn about the interface in Java and where do we use. Moreover, we will study the Java Interface example, its features, and advantages of the interface in Java Programming Language.

So, let’s start Interface in Java.

What is Interface in Java

What is Interface in Java – Features of JDK 8 & JDK 9

2. What is Interface in Java?

Interfaces in Java are used in many places, they can have methods just as Java class with the difference that they will be abstract by default. It specifies what a class should do and not how it should do. An interface in java has capabilities to perform a method, it basically has a set of methods that class may or may not apply.

A class declared abstract only when a class implements an interface and then it doesn’t allow the bodies of methods to the functions. It can also use as a sorting tool for collections when a class implements the Java interface ‘Comparator interface’ which is available in the Java library.

Do you know about Java Exception – Types & Java Catch Exceptions

Syntax 

interface <interface_name>
{
// declare constant fields declare methods that abstract by default.
}

For declaration of an interface in Java, we use the keyword ‘interface’ and for implementing it we use the keyword ‘implements’.

An interface in Java provides the user to have a total abstraction, which means that all the methods are declared empty body and as public. The fields must be final, public and static.

3. Advantages of Interface in Java

  1. For achieving total abstraction.
  2. Multiple inheritances can be achieved by an interface in Java.
  3. Loose coupling is done using interfaces in Java.
Java Quiz

4. Java Interface Example

Let’s discuss some example of Interface:

a. Example- 1

import java.io.*;
// A simple interface
interface in1
   {
// public, static and final
      final int a = 10;
// public and abstract
     void display();
   }
// A class that implements an interface.
class testClass implements in1
    {
// Implementing the capabilities of interface.
      public void display()
          {
             System.out.println("Geek");
          }
// Driver Code
public static void main (String[] args)
       {
          testClass t = new testClass();
          t.display();
          System.out.println(a);
       }
   }

Let’s explore the Career Opportunities in Java

b. Example- 2

import java.io.*;
interface Vehicle
   {
// all are the abstract methods.
     void changeGear(int a);
     void speedUp(int a);
     void applyBrakes(int a);
   }
class Bicycle implements 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)
     {
        speed = speed + increment;
     }
// to decrease speed
@Override
public void applyBrakes(int decrement)
     {
        speed = speed - decrement;
     }
public void printStates()
     {
         System.out.println("speed: " + speed
         + " gear: " + gear);
     }
 }
class Bike implements 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)
     {
         speed = speed + increment;
     }
// to decrease speed
@Override
public void applyBrakes(int decrement)
     {
        speed = speed - decrement;
     }
public void printStates()
     {
         System.out.println("speed: " + speed
         + " gear: " + gear);
     }
  }
class GFG
   {
       public static void main (String[] args)
         {
// creating an inatance of Bicycle doing some operations
           Bicycle bicycle = new Bicycle();
           bicycle.changeGear(2);
           bicycle.speedUp(3);
           bicycle.applyBrakes(1);
           System.out.println(" present state of bicycle  :");
           bicycle.printStates();
// creating instance of bike.
           Bike bike = new Bike();
           bike.changeGear(1);
           bike.speedUp(4);
           bike.applyBrakes(3);
           System.out.println(" present state of bike :");
           bike.printStates();
       }
  }

5. Features of Interfaces in JDK 8

Before we discuss features of JDK 8, let’s revise Features of Java

a. Defining Implementation

We can define default implementation with JDK 8 which has special uses and it also does not change the working of an interface.

Example

interface in1
     {
        final int a = 10;
        default void display()
            {
                System.out.println("hello");
            }
     }
// A class that implements interface.
class testClass implements in1
     {
// Driver Code
        public static void main (String[] args)
             {
                 testClass t = new testClass();
                 t.display();
             }
      }

b. Defining Static Methods

We can define static methods which don’t require any object for calling.
Example 

interface in1
   {
      final int a = 10;
      static void display()
         {
            System.out.println("hello");
         }
    }
// A class that implements interface.
class testClass implements in1
     {
// Driver Code
         public static void main (String[] args)
               {
                  in1.display();
               }
      }

6. Features of Interface in JDK 9

JDK has the following features:

  1. Static methods
  2. Private methods
  3. Private Static methods

So, this was all about Java Interface. Hope you like our explanation.

7. Conclusion

Hence, we learned about the various aspects of Java interfaces, how they are used, Implementation of Java interface with evolution and interface in java with an example program. Furthermore, if you feel any query, feel free to ask in the comment section.

See Also- Encapsulation in Java & Java Syntax

For reference

No Responses

  1. vamsi says:

    please upload the progeram based on soft ware using interface

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.