Interface in Java – Uncover the Difference Between Classes and Interfaces

We heard Interface in Java in many of our articles but don’t know the exact meaning of it. An Interface is a medium through which two systems meet and interact. For example, you might have used a vending machine to select an item, pay for that item and select the drink.

From programming, the perspective interface is between the software components. It is much similar to a Java class the only difference is that it has static constants and abstract methods. In Java, Interface is mainly used to implement multiple inheritances.  It is one of the core concepts of Java programming and it is used a lot in Java design patterns.

What is an Interface in Java with example

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 be used as a tool for sorting the collections when a class implements the Java interface ‘Comparator interface’ which is available in the Java library.

Syntax of Java Interface

Interface <interface-name>
{
//methods
}

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. What is the use of the Interface in Java?

As mentioned above is used for full abstraction. Therefore, the methods in interfaces do not contain the body, they have to be implemented by the class itself before accessing them.

The class which is implementing interface must implement all the methods of the interface. Java doesn’t allow you to extend multiple classes, but you can implement more than one interface in your class.

1.1 Advantages of Interface in Java

These are the main uses or advantages of an Interface

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

 Advantages of Interface in Java

1.2 Important Points about Interface

  1. An interface in Java provides abstraction and abstract classes can have method implementations but it can’t have the implementation of methods.
  2. implements keyword is used to implement an interface.
  3. We can’t instantiate an interface.
  4. It can’t have constructors, because we can’t instantiate constructor.
  5. An attribute is by default public, static and final, so we don’t need to provide access modifiers and if we do, the compiler doesn’t complain about it either.
  6. Interface methods are implicitly abstract and public, it makes sense because the methods of the interface don’t contain the body and so, therefore, the subclasses can provide the implementation.
  7. Interface itself cannot extend any class but it may extend another interface.
  8. A class that is implementing interface must provide a definition of all of its methods unless it is an abstract class.

Don’t forget to check the Constructor in Java with Real-time Example

2. Java Interface Example

package com.dataflair.interfaceprogram;
// Interface
interface Animal 
{
      public void animalSpecies(); // interface method (does not have a body)
      public void animalSound(); // interface method (does not have a body)
      public void animalHorns(); // interface method (does not have a body)

}
// Cow class "implements" the Animal interface
class Cow implements Animal 
{
      public void animalSpecies() 
       {
             // The body of animalSound() is provided here
             System.out.println("The Species of Cow is: Angus Cattle");
       }
public void animalSound() 
{
      // The body of animalSound() is provided here
      System.out.println("The Cow says: moo moo");
}
public void animalHorns() {
      // The body of animalHorns() is provided here
      System.out.println("Cow has 2 horns");
      }
}
//SwampDeer class "implements" the Animal interface
class SwampDeer implements Animal 
{
      public void animalSpecies() 
       {
            // The body of animalSound() is provided here
            System.out.println("The Species of Swamp Deer is: Deer");
}
public void animalSound() 
      {
            // The body of animalSound() is provided here
            System.out.println("The Swamp Deer says: bleat bleat");
      }
      public void animalHorns() {
      // The body of animalHorns() is provided here
      System.out.println("Swamp Deer has 12 horns");
      }
}

public class InterfaceDemo {
      public static void main(String[] args) {
            Cow myCow = new Cow(); // Create a Cow object
            SwampDeer mySwampDeer=new SwampDeer(); // Create a SwampDeer object

            //calling methods of class cow
            myCow.animalSpecies();
            myCow.animalSound();
            myCow.animalHorns();

            // calling methods of class SwampDeer
            mySwampDeer.animalSpecies();
            mySwampDeer.animalSound();
            mySwampDeer.animalHorns();
       }
}

Output-

Java Interface with real-time example

3. Relationship Between Classes and Interface

In the following figure, we can see that a class can extend another class, and an interface can extend another interface, but a class implements the interface.

Relationship Between Classes and Interface

Java Quiz

4. Difference Between Class and Interface in Java

Difference Between Class and Interface

5. Summary

Interfaces are similar or a blueprint of classes but not exactly matched, they have many differences. In general words, we can say the interfaces have variables and abstract methods. Hope, you understood the concept with an example program. Please share your reviews and suggestions in the comment box.

To master the language, DataFlair provide you a free tutorial series of Java

2 Responses

  1. vamsi says:

    please upload the progeram based on soft ware using interface

  2. james marandi says:

    sir i like to your concept about java deffernts topic… it is too much helpful for me

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.