Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Interface Program in Java

Last Updated on September 22, 2023 by Mayank Dham

In the following article, our focus will be on understanding the concept of inheritance in Java, exploring the practical applications of interface in Java, delving into the evolutionary enhancements made to interfaces across different Java versions, and finally, we will examine a Java program that demonstrates the implementation of an interface through a practical example.

What is an interface in java?

The main purpose of the interface in java is to provide security through an abstraction. An interface refers to the blueprint of the class. An interface contains abstract and static methods in it.

The interface is used to achieve 100% abstraction in java. This is because java interfaces only contain abstract methods.

What are abstract methods?

Abstract methods are a type of method which do not have the implementation of the method. This means, it does not have a body, it only contains a declaration.

One more additional application of the java interfaces is that we can achieve multiple inheritances with help of an interface which is not possible to achieve using classes.

What are the usages of the java interface?

  • The java interfaces are used to achieve abstraction in java
  • To achieve loose coupling the java interface is used.
  • With help of the java interfaces, we can implement multiple inheritance in java

Improvement in an interface by java versions:

Below is some improvement in the java interface according to java versions.

  • Java 8: Addition of the static and default methods in the java interface. The default method becomes very useful to achieve multiple inheritance in java.
  • Java 9: Addition of the private methods in the java interface. This will provide more security in abstraction.

Declaration of the java interface:

The interface keyword is used to declare the java interface. The java interfaces focus on security by providing full abstraction. In the interface, all the methods have empty bodies, and all the fields and members are public, static, and final by default. Let’s see the syntax to define the interface in java.

interface interface_name{
    // define all the abstract methods
    // declare constant fields
}

In the above syntax, we have defines an interface using the interface keyword followed by the name of an interface. Inside the interface, we can define all the abstract methods and we can also declare constant fields inside it. Let’s see an interface program in java to understand it better.

// interface program in java
interface MyInterface{
    void test(); 
}

class InterfaceImpl implements MyInterface{
    public void test(){
        System.out.println("Welcome to the interface program in java...");
    }
}

class HelloWorld {
    public static void main(String[] args) {
        InterfaceImpl impl= new InterfaceImpl();
        impl.test();
    }
}

Output:

Welcome to the interface program in java…

In the above example, we have created an interface ‘MyInterface’ with a method test without a body. We also created a class InheritanceImpl’ which implements an interface ‘MyInterface’ and in this class, we also defined the body of the method test. After that, we created an object of class ‘InterfaceImpl’ and called the method test. In the output, we can see that we are successfully able to call the test method.

Let’s see some useful relations to know while extending classes of implementing interfaces.

If we want to inherit one class from another class we can use the keyword extends.

If we want to inherit one class from an interface we can use the keyword Implements.

If we want to inherit one interface from another interface we can use the keyword extends.

Interface program in java with examples

Now, we already know what is interface and how to define an interface in java. Let’s see examples of interface programs in java.

Example 1: Let’s take an example of colors to understand how interfaces work.

// interface program in java
interface Color{
    void print_color();
    default void default_color(){
        System.out.println("This is default color");
    } 
}

class Red implements Color{
    public void print_color(){
        System.out.println("The color is Red");
    }
}

class HelloWorld {
    public static void main(String[] args) {
        Color red= new Red();
        red.print_color();
        red.default_color();
    }
}

Output:

The color is Red
This is the default color

In the above interface program in java, we have created an interface Color with an abstract method print_color and a default method default_color. After that, we inherit that interface in the class Red and implemented a method print_color in that class. In the output, we can see that both methods are printed.

Example 2: Let’s take a real-world example of the bank to understand interface program in java.

// interface program in java
interface Bank{
    int rate_of_interest();
}

class BOB implements Bank{
    public int rate_of_interest(){
        return 7;
    }
}

class ICICI implements Bank{
    public int rate_of_interest(){
        return 8;
    }
}

class PrepBytes {
    public static void main(String[] args) {
        int amount=1000;
        double interest_amount;
        Bank bob=new BOB();
        int bob_rate=bob.rate_of_interest();
        interest_amount=(amount*bob_rate)/100;
        
        System.out.println("Interest amount of the bank BOB is: "+interest_amount);
        
        Bank icici=new ICICI();
        int icici_rate=icici.rate_of_interest();
        interest_amount=(amount*icici_rate)/100;
        
        System.out.println("Interest amount of the bank ICICI is: "+interest_amount);
        
    }
}

Output:

Interest amount of the bank BOB is: 70.0
Interest amount of the bank ICICI is: 80.0

In the above interface program in java, we have created an interface Bank with an abstract method rate_of_interest. After that, we inherit that interface in the class BOB and ICICI and implemented a method rate_of_interest in that class. In the output, we can see that first we calculated interest for the bank BOB and printed it. After that, we calculated the interest for ICICI and printed it.

How to achieve multiple inheritance in java using the interface and default method:
We can achieve multiple inheritance in java using the interface and default method after it was introduced in java 8. After this feature, a class can inherit two or more than two parent classes with the same methods. We can also override the superclass method in the subclass. Let’s see an interface program in java to achieve multiple inheritance as an example.

Example 1:

// multiple inheritance program in java

interface Car{
    default void test(){
        System.out.println("Testing a car");
    }
}
interface Bike{
    default void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes implements Car,Bike{
    @Override
    public void test(){
        Car.super.test();
        Bike.super.test();
    }
    
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

Output:

Testing a car
Testing a bike

In the above example, first, we created two interfaces Car and Bike with the same default method test. In the child class Prepbyte, we have overridden the test method and inside it, we called both methods using the super keyword. In the output, we can see that the methods of both interfaces are called in the sub-class.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java
Number Pattern Program in Java
For Loop Program In Java
Pattern Program in Java
String Palindrome Program in Java
Thread Program in JAVA
Java Scanner Program
While Loop Program in Java
Bubble Sort Program in Java
Fibonacci Series Program in Java

Leave a Reply

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