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

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
Something went wrong!
Download App & Start Learning
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. 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 Demo Classes

Register here for Free Demo Classes

Trending Courses

Professional Certification Programme in Digital Marketing (Batch-5)
Professional Certification Programme in Digital Marketing (Batch-5)

Now at just ₹ 49999 ₹ 9999950% off

Master Certification Digital Marketing Program Batch-10
Master Certification Digital Marketing Program Batch-10

Now at just ₹ 64999 ₹ 12500048% off

Advanced Certification in Digital Marketing Online Programme (Batch-22)
Advanced Certification in Digital Marketing Online Programme (Batch-22)

Now at just ₹ 20999 ₹ 3599942% off

Advance Graphic Designing Course (Batch-9) : 90 Hours of Learning
Advance Graphic Designing Course (Batch-9) : 90 Hours of Learning

Now at just ₹ 15999 ₹ 3599956% off

Flipkart Hot Selling Course in 2024
Flipkart Hot Selling Course in 2024

Now at just ₹ 10000 ₹ 3000067% off

Advanced Certification in Digital Marketing Classroom Programme (Batch-3)
Advanced Certification in Digital Marketing Classroom Programme (Batch-3)

Now at just ₹ 29999 ₹ 9999970% off

Basic Digital Marketing Course (Batch-24): 50 Hours Live+ Recorded Classes!
Basic Digital Marketing Course (Batch-24): 50 Hours Live+ Recorded Classes!

Now at just ₹ 1499 ₹ 999985% off

WhatsApp Business Marketing Course
WhatsApp Business Marketing Course

Now at just ₹ 599 ₹ 159963% off

Advance Excel Course
Advance Excel Course

Now at just ₹ 2499 ₹ 800069% off