java_interfacing
java_interfacing
Introduction:
• An interface is a fully abstract class. It includes a group
of abstract methods (methods without a body).
• We use the interface keyword to create an interface in Java.
For example,
Here,
•Language is an interface.
•It includes abstract methods: getType() and getVersion().
Contd.
• To access the interface methods, the interface must be "implemented" like inherited) by
another class with the implements keyword (instead of extends).
• The body of the interface method is provided by the "implement" class:
Implementing an Interface
• Like abstract classes, we cannot create objects of interfaces.
• To use an interface, other classes must implement it. We use the implements
keyword to implement an interface.
• Example 1: Java Interface
interface Polygon { class Main {
void getArea(int length, int breadth); public static void main(String[] args) {
} Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
// implement the Polygon interface }
class Rectangle implements Polygon { }
interface A {
// members of A
}
interface B {
// members of B
}
class C implements A, B {
// abstract members of A // abstract members of B
}
Example:
OUTPUT:
Some text..
Some other text...
Extending an Interface
• Similar to classes, interfaces can extend other interfaces. The extends keyword is
used for extending interfaces. For example,