Site icon DED9

Interface Training In Java (In Quite Simple Language)

In this tutorial, we will learn about Java interfaces and learn how to implement interfaces and when to use them.

In Java, an interface defines a set of attributes that other classes must implement.

For example,

  1. Polygon interface {
  2. public void getArea ();
  3. }

Here, Polygon is an interface. We used the keyword interface to define the interface.

The getArea () method is a property specified in the Polygon interface. All classes that use this interface must implement the getArea () method.

The interface can include abstract and fixed methods. For example,

  1. Polygon interface {
  2. public static final String color = “blue”;
  3. public void getArea ();
  4. }

In the example above, we have created a Polygon interface that includes the color constant variable and the abstract method getArea ().

It is important to note that all methods within an interface are public by default, and all fields are public static by default. Therefore, it is not necessary to specify the access level within the interfaces. For example, we can write the above code as follows:

  1. Polygon interface {
  2. String color = “blue”;
  3. void getArea ();
  4. }

Keyword implements in the interface

Like abstract classes, we cannot create objects from the interface. However, we can implement interfaces in other classes. In Java, we use the keyword implements to implement interfaces. For example,

  1. Polygon interface {
  2. void getArea ();
  3. }
  4. class Rectangle implements Polygon {
  5. public void getArea (int length 5, int breadth = 6) {
  6. int length = 5;
  7. int breadth = 6;
  8. System.out.println (“The area of ​​the rectangle is” + (length * breadth));
  9. }
  10. }
  11. class Main {
  12. public static void main (String [] args) {
  13. Rectangle r1 = new Rectangle ();
  14. r1.getArea (5, 6);
  15. }
  16. }

Output

The area of ​​the rectangle is 30

In the above program, we have created a Polygon interface. The Polygon interface has an abstract method getArea ().

This means that each class that implements Polygon must provide an execution for the getArea () method.

Note that the Rectangle class (which implements the Polygon interface) has the getArea () method implemented.

Why do we use interfaces?

Now that we know what interfaces are, let’s learn why interfaces are used in Java.

Interfaces provide properties that a class (which uses it) must follow.

In the example above, we used getArea () as a property inside the Polygon interface. This is like setting a rule that we should be able to get the area of ​​a polygon. Therefore, any class that implements the Polygon interface must provide an execution for the getArea () method.

Similar to abstract classes, interfaces help us achieve abstraction in Java. Here we know that getArea () calculates the area of ​​polygons, but how to calculate the area for different polygons is different. Hence, the getArea () implementation is independent of each other.

Interfaces are also used to achieve multiple inheritance in Java. If a subclass inherits from two or more classes, it is multiple inheritance.

In Java, multiple inheritance is not possible. However, a class can implement multiple interfaces that allow us to achieve multiple inheritance functionality in Java. For example,

  1. interface Line {
  2. }
  3. Polygon interface {
  4. }
  5. class Rectangle implements Line, Polygon {
  6. }

Here, Rectangle must implement all Line and Polygon methods.

Private and static methods in the interface

With the release of Java 8, interfaces can now include static methods.

Like a class, we can access an interface using references to static methods. For example,

  1. Polygon.staticMethod ();

Interfaces also support private methods with the release of Java 9. You can now use private methods and private static methods in interfaces.

Because you can not define interfaces, private methods are used as auxiliary methods that provide support for other interface methods.

Default methods in interfaces

With the release of Java 8, implementation methods (default methods) were introduced within an interface. Before that, all methods in Java were abstract.

To define the default methods in the interfaces, we use the default keyword. For example,

  1. public default void getSides () {
  2. // body of getSides ()
  3. }

Why default methods?

Let’s find out with a scenario why the default methods are introduced in Java.

Suppose we need to add a new method to an interface.

We can easily add the method in the user interface, without execution. However, this is not the end of the story. All classes that implement that interface must provide method implementation.

If a large number of classes were running this interface, we would have to follow all of these classes and make changes to them. This is not only tedious but also error prone.

To solve this problem, Java introduced the default methods. Default methods are inherited like regular methods.

Let’s take an example to better understand the default methods.

Example 2: Default method

  1. Polygon interface {
  2. void getArea ();
  3. default void getSides () {
  4. System.out.println (“I can get sides of polygon.”);
  5. }
  6. }
  7. class Rectangle implements Polygon {
  8. public void getArea () {
  9. int length = 6;
  10. int breadth = 5;
  11. int area = length * breadth;
  12. System.out.println (“The area of ​​the rectangle is” + area);
  13. }
  14. public void getSides () {
  15. System.out.println (“I have 4 sides.”);
  16. }
  17. }
  18. class Square implements Polygon {
  19. public void getArea () {
  20. int length = 5;
  21. int area = length * length;
  22. System.out.println (“The area of ​​the square is” + area);
  23. }
  24. }
  25. class Main {
  26. public static void main (String [] args) {
  27. Rectangle r1 = new Rectangle ();
  28. r1.getArea ();
  29. r1.getSides ();
  30. Square s1 = new Square ();
  31. s1.getArea ();
  32. }
  33. }

Output

The area of ​​the rectangle is 30

I have 4 sides

The area of ​​the square is 25

In the example above, we created the Polygon () interface. Polygon has a default getSides () method and an abstract getArea () method.

The Rectangle class then implements the Polygon. The Rectangle provides an implementation for the abstract getArea () method and ignores the default getSides () method.

We have created another Square class that also implements Polygon. Here, Square only offers the execution of the getArea () abstract method.

Practical interface example

Let’s look at a more practical example of a Java interface.

  1. // To use the sqrt function
  2. import java.lang.Math;
  3. Polygon interface {
  4. void getArea ();
  5. // calculate the perimeter of a Polygon
  6. default void getPerimeter (int… sides) {
  7. int perimeter = 0;
  8. for (int side: sides) {
  9. perimeter + = side;
  10. }
  11. System.out.println (“Perimeter:” + perimeter);
  12. }
  13. }
  14. class Triangle implements Polygon {
  15. private int a, b, c;
  16. private double s, area;
  17. // initializing sides of a triangle
  18. Triangle (int a, int b, int c) {
  19. this.a = a;
  20. this.b = b;
  21. this.c = c;
  22. s = 0;
  23. }
  24. // calculate the area of ​​a triangle
  25. public void getArea () {
  26. s = (double) (a + b + c) / 2;
  27. area = Math.sqrt (s * (sa) * (sb) * (sc));
  28. System.out.println (“Area:” + area);
  29. }
  30. }
  31. class Main {
  32. public static void main (String [] args) {
  33. Triangle t1 = new Triangle (2, 3, 4);
  34. // calls the method of the Triangle class
  35. t1.getArea ();
  36. // calls the method of Polygon
  37. t1.getPerimeter (2, 3, 4);
  38. }
  39. }

Output

Area: 2.9047375096555625

Perimeter: 9

In the above program, we have created a Polygon interface that includes a default getParameter () method and an abstract getArea () method.

We can calculate the circumference of all polygons in the same way, so we implemented the getPerimeter () body in Polygon. Now, all polygons that implement Polygon can use getPerimeter () to calculate the environment.

However, the environment is calculated differently for different polygons because the rule for calculating the environment is different for different polygons. Hence, getArea () is included in Polygon without execution. And any class that implements the Polygon interface must implement getArea ().

Keyword extends in the interface

Like classes, interfaces can develop other interfaces. The extends keyword is used to extend interfaces. For example,

  1. interface Line {
  2. // members of Line interface
  3. }
  4. interface Polygon extends Line {
  5. // members of Polygon interface and Line interface
  6. }

In the example above, the Polygon interface extends the Line interface. Now if it implements a Polygon class, it must execute all the abstract Line and Polygon classes.

Note that an interface can extend multiple interfaces similar to a class that executes multiple interfaces. For example,

  1. interface A {
  2. }
  3. interface B {
  4. }
  5. Interface C extends A, B {
  6. }
Exit mobile version