Menu

Interface in Java

In Java programming, an interface is a blueprint that specifies the behavior of a class. It defines a set of methods that classes implementing the interface must implement. Interfaces promote abstraction and loose coupling in your code.

What Interfaces Do

  1. Declare methods: Interfaces define methods without providing their implementation details. These methods act as contracts that implementing classes must fulfill.

  2. Promote abstraction: By focusing on what a class can do (methods) rather than how it does it (implementation), interfaces encourage a more abstract and reusable design.

  3. Enable polymorphism: Interfaces allow objects of different classes that implement the same interface to be treated interchangeably. This is because they all guarantee the availability of the same set of methods.

Types of Interfaces in Java

While Java doesn't have explicitly named interface types, there are different ways interfaces can be used and categorized:

  1. Regular Interfaces: These are the most common type. They simply define methods without implementation. Classes implementing them must provide concrete implementations for all the declared methods.

  2. Functional Interfaces (Java 8+): Introduced in Java 8, functional interfaces have exactly one abstract method. They are used extensively with lambda expressions and method references for functional programming capabilities.

  3. Marker Interfaces: These interfaces don't declare any methods. They act as flags or markers to indicate that a class has a certain characteristic or functionality. The Serializable and Cloneable interfaces are examples. The Java runtime environment or other tools might use marker interfaces for specific purposes.

  4. Default Interfaces (Java 8+): Interfaces can now include method implementations starting from Java 8. These methods are called default methods and can provide default behavior for a method. Implementing classes can override default methods if they need different behavior.

Java Interface Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
interface BaseCar {
    String getBrand(); // Declares a method to get the car brand (abstract)
    String getModel(); // Declares a method to get the car model (abstract)
}

interface MarutiCar extends BaseCar { // Extends BaseCar interface
    String getColor(); // Declares a method to get the car color (abstract)
}

class MarutiSwift implements MarutiCar { // Implements MarutiCar interface

    private String brand = "Maruti";
    private String model = "Swift";
    private String color;

    public MarutiSwift(String color) { // Constructor to set color
        this.color = color;
    }

    @Override
    public String getBrand() {
        return brand;
    }

    @Override
    public String getModel() {
        return model;
    }

    @Override
    public String getColor() {
        return color;
    }
}

public class Main {

    public static void main(String[] args) {
        MarutiCar car = new MarutiSwift("Red"); // Create a MarutiSwift object

        System.out.println("Car Brand: " + car.getBrand()); // Call getBrand() from BaseCar
        System.out.println("Car Model: " + car.getModel()); // Call getModel() from BaseCar
        System.out.println("Car Color: " + car.getColor()); // Call getColor() from MarutiCar
    }
}
  1. ABC
  2. ABC
  3. ABC
  4. ABC

No comments:

Post a Comment