Java is an object-oriented programming language that provides several features to build robust and scalable applications. Among these features, interfaces are an essential part of Java programming, and they play a significant role in defining the behavior of Java classes. In this article, we will discuss interfaces in Java, their purpose, and how they differ from abstract classes. We will also provide code examples and their output to illustrate the concept of interfaces. If you are new to Java programming and want to learn more about abstract classes or inheritance, we suggest you read our articles on those topics first.
What is Interface?
- In Java, an interface is a collection of abstract methods and constants that can be implemented by a class. An interface provides a way to define a contract between multiple classes without specifying their implementation details. Interfaces are similar to classes, but they cannot be instantiated directly.
- An interface is a collection of abstract methods and constants.
- An interface is used to achieve abstraction in Java.
- An interface is a contract between two classes where one class promises to provide the implementation of the abstract methods defined in the interface.
- Interfaces are used to define a set of methods that a class must implement.
- Interfaces can be used to implement multiple inheritance in Java.
- The methods in an interface are by default abstract and public.
- Interfaces can be implemented by any class, regardless of its position in the class hierarchy.
- Interfaces can be used to provide a common interface that can be used by different classes.
- An interface can be used to define a group of related methods that a class can implement.
Interfaces can be used to define constants that are used throughout an application. - You should create an interface when you want to define a common set of methods that multiple classes must implement, but you don’t care about the details of how those methods are implemented. This allows you to write more flexible and modular code, because you can write code that works with any class that implements a particular interface, without knowing the specific implementation details of each class.
How to Declare an Interface?
You can declare an interface using the following syntax:
public interface MyInterface { public void method1(); public void method2(); public static final int MY_CONSTANT = 42; }
In this example, the interface is named “MyInterface”. It contains two abstract methods, “method1” and “method2”, which are not implemented within the interface. It also contains a constant, “MY_CONSTANT”, which is declared as static and final. All variables in an interface are implicitly public, static, and final.
To implement an interface in a class, the “implements” keyword is used in the class declaration, followed by the name of the interface. Here’s an example:
public class MyClass implements MyInterface { public void method1() { // implementation goes here } public void method2() { // implementation goes here } }
In this example, the class “MyClass” implements the “MyInterface” interface by providing implementations for the “method1” and “method2” methods. It can also access the constant “MY_CONSTANT” defined in the interface.
Interfaces can be used to achieve polymorphism, where multiple classes can be used interchangeably because they implement the same interface. This is particularly useful in situations where you want to decouple the implementation details of a class from its public interface.
Advantages of Interface :
Abstraction: Interface provides a high level of abstraction, which helps in separating the behavior of a class from its implementation.
Multiple Inheritance: Java does not support multiple inheritance of classes, but it allows multiple inheritance of interfaces. This feature allows a class to inherit from multiple interfaces, which helps in achieving polymorphism.
Loose Coupling: Interface promotes loose coupling between classes, which means changes made to one class will not affect the other classes that use it. This makes the code more maintainable and extensible.
Consistency: Interfaces provide a consistent way of defining behavior, which helps in maintaining consistency across different implementations.
Disadvantages of Interface:
Complexity: Sometimes, the use of interfaces can add complexity to the code, especially when there are many interfaces and their implementations.
Extra Code: Interfaces require extra code to define and implement, which may add to the development time.
Indirection: Indirection occurs when the code needs to go through multiple layers of abstraction to reach the actual implementation, which may affect performance.
Limited Access: Interfaces only define behavior and cannot contain any implementation, which may limit the access to certain features.
If I am using an interface to create only abstract methods, then why am I not using abstract classes? What are the differences between abstract classes and interfaces?
Difference between Abstract Classes and Interfaces :
Feature | Abstract Class | Interface |
---|---|---|
Method implementation | Can have both implemented and unimplemented methods | Can only have unimplemented (abstract) methods |
Multiple inheritance | Cannot inherit multiple abstract or concrete classes | Can inherit multiple interfaces |
Access modifiers | Can have public, protected, private, or default access | Methods are implicitly public, fields are implicitly public static final |
Constructor | Can have a constructor | Cannot have a constructor |
Variables | Can have instance variables and constants | Can only have constants (public static final variables) |
Extensibility | Can be extended using “extends” keyword | Can be implemented using “implements” keyword |
Use cases | Used when creating a hierarchy of classes | Used when defining a contract between classes |
Can an interface be extended?
Yes, an interface can be extended in Java using the extends
keyword, similar to how a class is extended.
When an interface extends another interface, it inherits all the abstract methods of the parent interface. The child interface can add additional abstract methods, but it must also implement all the methods from the parent interface.
Here’s an example of how to extend an interface in Java:
public interface ParentInterface { public void method1(); public void method2(); } public interface ChildInterface extends ParentInterface { public void method3(); }
In this example, the ChildInterface
extends the ParentInterface
using the extends
keyword. As a result, ChildInterface
inherits the method1()
and method2()
methods from the ParentInterface
, and it adds its own method3()
method. Any class that implements ChildInterface
must implement all three methods: method1()
, method2()
, and method3()
class MyClass implements ChildInterface { public void method1() { System.out.println("Implementing method1 from ParentInterface"); // Implementation of method1 } public void method2() { System.out.println("Implementing method2 from ParentInterface"); // Implementation of method2 } public void method3() { System.out.println("Implementing method3 from ChildInterface"); // Implementation of method3 } }
public class TestInterface { public static void main(String[] args) { MyClass obj = new MyClass(); obj.method1(); obj.method2(); obj.method3(); } }
Save the above code as “TestInterface”. After saving it, compile and run the code. You will get the below output.
Implementing method1 from ParentInterface Implementing method2 from ParentInterface Implementing method3 from ChildInterface
For more practice on problems related to interfaces, please refer to the article titled ‘Practice Problems on Interface’.
In conclusion, interfaces are an important part of Java programming that allows for the implementation of multiple inheritance and abstraction. They offer several advantages such as enforcing standards and providing flexibility in implementation. However, they also have some disadvantages such as the need for additional code and potential for complexity.
It is important to understand the differences between interfaces and abstract classes and when to use each. Interfaces can be extended to create new interfaces, providing even more flexibility and customization options.
Stay connected for our next article on “What is Package in Java”, where we will discuss how to organize and manage classes in Java using packages.