Table of contents
1.
Introduction
2.
What is Interface?
3.
Properties of Interface:
4.
Why do we use Interface:
5.
Declaring Interfaces:
6.
Implementing Interfaces
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Interfaces

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Interface in Java is a way to achieve abstraction. It is a class blueprint with constants and abstract methods. It is an entirely abstract class used to group related methods with empty bodies. It is a reference type in Java that is similar to a class.

Also see, Duck Number in Java and Hashcode Method in Java

What is Interface?

An interface is a blueprint of a Java class. A class implements an interface by inheriting the abstract methods of the Interface. An interface may also contain constants, default, static, and nested methods. 

Properties of Interface:

  • An interface might contain any number of methods.
  • It is written in the .java extension, with the Interface's name matching the file's name.
  • The byte code of an interface that appears is a .class file.
  • Interfaces that appear in packages are their corresponding bytecode file.

Why do we use Interface:

  • It is used to get total abstraction.
  • By Interface, we can support multiple inheritances (a feature of object-oriented programming, where a base class inherits properties of one and more than one parent class).
  • It can be used to achieve loose coupling(when an object gets an object to be used from the outside).
  • Interfaces are used to implement abstraction. These abstract members should be given the implementation under a child class or Interface.

Declaring Interfaces:

With the help of keywords, the Interface declares an interface. Here is a simple example showing how to declare an interface-

Interface Human()
 { 
public void growth();
 public void die(); 
}
You can also try this code with Online Java Compiler
Run Code

Implementing Interfaces

When Interface gets implemented, you can assume the class is making a contract, agreeing to act according to the specific behaviors of the Interface. If a class does not respond to all the interface behaviors, it must declare itself an abstract.

public class Human implements Nature {
 public void eat() { 
System.out.println("Human eats"); 
} 
public void travel() {
 System.out.println("Human travels");
 } 
public int noOfLegs() {
 return 0; 
} 
public static void main(String args[]) {
 Human h = new Human();
 h.eat();
 h.travel();
 }
 }
You can also try this code with Online Java Compiler
Run Code

Output:

Human eats
Human Travels
You can also try this code with Online Java Compiler
Run Code

Here in this code, we have implemented the interface using two simple functions the first one is eat and the second one is travel then we have created an object of the class to call the functions. Try this code by yourself on Online Java Compiler.

Know about Single Inheritance in Java in detail.

Frequently Asked Questions

  • What is allowed in a Java Interface?

An interface can have constants; these constants are public, static, and final(Implicitly). A class that implements an interface might be abstract. An abstract class implementing an interface need not implement all abstract methods. A class can implement more than one Interface.

  • Can the Java interface have variables?

An interface can have methods and variables as a class, but those methods declared in an interface are by default abstract (only method signature, without body). Interfaces specify what a class must do and not how.

Key Takeaways

In this blog, we have covered Java interfaces, and the key points are given below:

  • Introduction to interfaces
  • What is an Interface?
  • Why do we use it?
  • How to declare and the rules for declaring it.
  • How to implement an interface. 

If you want to learn more about Interfaces, you can visit our website, Interfaces

 

Live masterclass