An interface serves as an abstract “class” utilized for organizing related methods, each lacking implementation details.
To utilize the methods defined in an interface, another class must “implement” it using the “implements” keyword (instead of “extends”). The implementing class furnishes the implementation details for the interface methods.
// interface
// Pig "implements" the Animal interface
|
The “implements” keyword is employed to enact an interface.
The “interface” keyword is utilized to declare a distinct class type that exclusively comprises abstract methods.
To utilize the methods defined in an interface, another class must “implement” it using the “implements” keyword (similar to inheritance), rather than “extends.” The implementing class supplies the implementation details for the interface methods.
Notes on Interfaces:
Why And When To Use Interfaces?To ensure security by concealing specific details and revealing only the essential aspects of an object (interface), Java employs interfaces. Java does not facilitate “multiple inheritance” (where a class inherits from more than one superclass). Nevertheless, this can be accomplished using interfaces, as a class can implement multiple interfaces. Remember: To implement multiple interfaces, list them with commas (as illustrated below). |
To implement multiple interfaces, list them separated by commas.
interface
}
interface
// DemoClass "implements" FirstInterface and SecondInterface
class |