How do you define an interface and how do you use it in Java. what happens if an abstract class implements an interface? Is it possible to implement an interface without using all the abstract methods in the implemented class?
How do you define an interface and how do you use it in Java. what happens if an
abstract class implements an interface? Is it possible to implement an interface
without using all the abstract methods in the implemented class? Elaborate your
answer with proper Examples and
explanations……………………………………………………..
An interface is like a blueprint of a class. It consists of static variables and abstract methods.
It is used for achieving abstraction. Only abstract methods are present. It is also used for achieving multiple inheritance.
The interface keyword is required for declaring an interface. Each method in an interface is declared empty, and all the data members by default are public, final and static. A class which implements an interface should implement each method of the interface.
Syntax:
interface <name>{
// declaration of constant fields
// declaration of abstract methods.
}
Example of interface:
interface printable{
void show();
}
class A implements printable{
public void show(){System.out.println("Hello");}
public static void main(String args[]){
A obj = new A();
obj.show();
}
}
Output:
Hello
Step by step
Solved in 2 steps