![]() |
VOOZH | about |
An interface in Java is a blueprint that defines a set of methods a class must implement without providing full implementation details. It helps achieve abstraction by focusing on what a class should do rather than how it does it. Interfaces also support multiple inheritance in Java.
Example: Defines constants and abstract methods, which are implemented by a class.
Geek 10
Notes:
- Private methods can only be called inside default or static methods.
- Static methods are accessed using the interface name, not via objects.
- To implement an interface, use the implements keyword.
A class can extend another class and similarly, an interface can extend another interface. However, only a class can implement an interface and the reverse (an interface implementing a class) is not allowed.
π Relationship between Class and InterfaceImplementation: To implement an interface, we use the keyword implements
Vehicles like bicycles, cars, and bikes share common functionalities that can be defined in an interface. Each class implements these in its own way, ensuring reusability, scalability, and consistency.
Bicycle present state : speed: 2 gear: 2 Bike present state : speed: 1 gear: 1
Java does not support multiple inheritance with classes to avoid ambiguity, but it supports multiple inheritance using interfaces.
Addition : 3 Substraction : 1
There are certain features added to Interfaces in JDK 8 update mentioned below:
hello
Another feature that was added in JDK 8 is that we can now define static methods in interfaces that can be called independently without an object. These methods are not inherited.
hello
From Java 9 onwards, interfaces can contain the following also:
Engine started. Vehicle is now driving.
One interface can inherit another by the use of keyword extends. When a class implements an interface that inherits another interface, it must provide an implementation for all methods required by the interface inheritance chain.