What Interfaces in JAVA and Why it is needed?

Safalta Expert Published by: Saksham Chauhan Updated Mon, 29 Aug 2022 11:52 PM IST

Highlights

Check What Interfaces in JAVA and Why it is needed? Here At Safalta.com

An interface is a class's blueprint in Java. It has static constants and abstract methods. The interface is a mechanism used by Java to achieve abstraction. Only abstract methods—not method bodies—are permitted in the Java interface. For multiple inheritance and abstraction in Java, it is employed.

Source: Safalta.com

In other words, interfaces have the ability to include variables and abstract methods. The technique body is impossible. The Java interface also serves as a representation of the IS-A connection.It cannot be instantiated, just like the abstract class.

Free Demo Classes

Register here for Free Demo Classes

Please fill the name
Please enter only 10 digit mobile number
Please select course
Please fill the email
Interfaces are allowed in Java 8 default and static methods. Private methods are allowed in interfaces as of Java 9. Download these FREE Ebooks:
1. Introduction to Digital Marketing
2. Website Planning and Creation

What is the need for an interface in Java?


For the following reasons, Java needs an interface:
  • To accomplish abstraction, it is employed.
  • We can offer the feature of multiple inheritance using an interface.
  • To achieve loose coupling, it might be employed.
The reason is that while variables in the interface are final, public, and static, abstract classes may have non-final variables.

How may an interface be declared?

The interface keyword is used to declare an interface. Total abstraction is provided; all methods in an interface are defined with an empty body, and all fields are by default public, static, and final. All of the methods defined in an interface must be implemented by a class that implements the interface.

Syntax for Java Interface

Interface {

//Declare Constant Fields;

//Declare Methods;

//Default Methods; 

 }

Example:
Here is a perfect Java interface example. Here, we attempt to compute the areas of geometric forms using several approaches, one for each shape. Additionally, each technique has been specified independently of the others. The Interface has just method signatures.

 

//Interface

package simplilearn;

public interface Area {

public void Square();

public void Circle();

public void Rectangle();

public void Triangle();

}

//Class

package simplilearn;

import java.util.Scanner;

public class shapeArea implements Area {

public void Circle() {

Scanner kb = new Scanner(System.in);

System.out.println("Enter the radius of the circle");

double r = kb.nextInt();

double areaOfCircle = 3.142 * r * r;

System.out.println("Area of the circle is " + areaOfCircle);

}

@Override

public void Square() {

// TODO Auto-generated method stub

Scanner kb2 = new Scanner(System.in);

System.out.println("Enter the length of the side of the square");

double s = kb2.nextInt();

double areaOfSquare = s * s;

System.out.println("Area of the square is " + areaOfSquare);

}

@Override

public void Rectangle() {

// TODO Auto-generated method stub

Scanner kb3 = new Scanner(System.in);

System.out.println("Enter the length of the Rectangle");

double l = kb3.nextInt();

System.out.println("Enter the breadth of the Rectangle");

double b = kb3.nextInt();

double areaOfRectangle = l * b;

System.out.println("Area of the Rectangle is " + areaOfRectangle);

}

@Override

public void Triangle() {

// TODO Auto-generated method stub

Scanner kb4 = new Scanner(System.in);

System.out.println("Enter the base of the Triangle");

double base = kb4.nextInt();

System.out.println("Enter the height of the Triangle");

double h = kb4.nextInt();

double areaOfTriangle = 0.5 * base * h;

System.out.println("Area of the Triangle is " + areaOfTriangle);

}

public static void main(String[] args) {

shapeArea geometry = new shapeArea();

geometry.Circle();

geometry.Square();

geometry.Rectangle();

geometry.Triangle();

}

}

//ExpectedOutput:

Enter the radius of the circle

15

Area of the circle is 706.9499999999999

Enter the length of the side of the square

12

Area of the square is 144.0

Enter the length of the Rectangle

10

Enter the breadth of the Rectangle

25

Area of the Rectangle is 250.0

Enter the base of the Triangle

25

Enter the height of the Triangle

30

Area of the Triangle is 375.0

Interface vs. Class Differences

The major differences between a class and an interface are:

S. No. Class Interface
1. You can instantiate variables and make objects in a class.  You cannot instantiate variables and make an object in an interface..
2.  A class may include concrete methods with implementation.  The interface is not permitted to include concrete (implementation-ready) methods.
3. Classes can employ the private, protected, and public access specifiers. Interface only uses the Public specifier.

Java interface disadvantages

Real-world projects either make considerable use of an interface or don't utilise one at all. The execution speed may be slower when an interface is used. Therefore, these were a few drawbacks of utilising an interface in Java. This brings us to the conclusion of the "Interface in Java" essay. We sincerely hope you liked learning about the fundamental Java interface principles. 

Also check,
Friend Function In C++ : Check Here To Know
Introduction to MS Excel- Basics, Features, Benefits, Sample Questions

 

Free E Books