Exploring Interfaces In Java

Imagine you're visiting a new city and want to explore its various attractions. You might come across different types of vehicles, such as buses, taxis, and auto-rickshaws. While these vehicles differ in appearance and size, they all share a common purpose: to transport passengers from one place to another.

An interface in Java works similarly. It defines a set of methods that a class must implement, regardless of its specific implementation details. These methods act like the essential functions that all vehicles must provide, such as starting, stopping, accelerating, and braking.

It defines service requirement specifications. For example, JDBC API acts as a requirement specification to develop a database driver. The database vendor is responsible for implementing this JDBC API.

It acts as a blueprint for a class, outlining the methods it must have and the functionality it should provide. Interfaces play a significant role in promoting code reusability, flexibility, and consistency in object-oriented programming.

Imagine you're designing a game with different characters, each with unique abilities. To ensure consistency and avoid code duplication, you create an interface called "Character" that defines the essential behaviors all characters must possess, such as moving, jumping, and attacking.

Now, every character class in the game, like "Hero" or "Monster," must implement the "Character" interface. This ensures that all characters share the same basic behaviors, while still allowing for their characteristics and abilities.

Advantages of an interface :

  1. Code Reusability: By defining a common set of methods, interfaces allow developers to reuse code across different classes, reducing the need for redundant code and promoting maintainability.

  2. Abstraction: Interfaces focus on the "what" rather than the "how," allowing developers to define the desired behavior without worrying about the specific implementation details. This promotes loose coupling between classes and enhances code flexibility.

  3. Consistency: Interfaces ensure that classes implementing them adhere to a consistent set of rules, making it easier for developers to understand and use different classes without having to learn their implementations.

  4. Polymorphism: Interfaces facilitate polymorphism, allowing objects of different classes to be treated uniformly as long as they implement the same interface. This enables developers to write generic code that works with various types of objects.

Methods in Interface :

  1. Every method in the interface is always public and abstract whether we are declaring or not.

    Public: to make every method available to all the classes that are implementing the interface whether it is in the package or outside the package.

    Abstract: The class that is implementing the interface is responsible for providing method implementation.

Naming Conflicts :

a). If two interfaces contain a method with the same signature and same return type then in the implementation class we have to provide an implementation of only one method.

b). For overloaded interface methods, we have to provide an implementation for each method in the implemented class.

c). If two interfaces contain methods with the same signature but different return types then it is impossible to implement both interfaces simultaneously until the return types of methods are not covariant.

Interface Variables:

  1. Interface variables help in defining requirement-level constants. Every interface variable is public static final.

    public: to make variables available to every implementation class.

    static: without an existing object, the implementation class can access that variable.

    Final: if one class changes then the remaining implementation class will be affected. To restrict this every interface variable is final.

  2. Because of the final modifier, it is compulsory to initialize the variable at the time of declaration. We could not use a static block for this purpose(Interface does not allow static block implementation).

  3. When a class implements an interface, then in that class we can not change the value of interface variables, the class can only access them.

  4. An ambiguity problem can occur when a particular class implements two interfaces simultaneously and both contain the same name variable. This problem can be solved by using an interface name to call interface variables with the same name.

Default Methods in Interface :

Default methods were introduced in Java 8. A default method is a method that has an implementation in the interface itself. This means that if a class implements the interface but does not provide its own implementation for a particular default method, then the class will inherit the implementation of the default method from the interface.