In this tutorial, we will learn about the Interfaces in Java. How to use them with the help of examples.
Define Interfaces in Java
Interface is pure-template class because it holds abstract method (no need to use abstract keyword) as well as final data. The Interface can be define using interface keyword. There is no individual meaning of interface class means we cannot create abject of the interface class. The importance of interface class with normal class by inherit it. This proves the concept of multiple inheritance because multiple inheritance can be inherited with a single class. Interface provides implements keyword for inheriting it with class. Interface is a always be public scope.
Syntax
Syntax of Syntaxinterface <interface_name>{ // declare constant fields // declare methods that abstract // by default. }
Why do use interface?
There are Following reasons to use java interface
- By interface, We can support the functionality of multiple inheritance.
- interface is use to achieve total abstraction.
- interface can be use to achieve loose coupling.
- Any class can extend only one class but can any class implement many number of interface.
Example of Interface
Input Value
Example of Interfaces in Javainterface company{ void print(); } class abms implements company{ public void print(){System.out.println("Basic Engineer");} public static void main(String args[]){ abms obj = new abms(); obj.print(); } }
Let’s run the code
Output Value
Multiple Inheritance in Java by Interface
We can achieve multiple inheritance through interfaces because interface contains only abstract method, which implementation is provided by the sub classes.
Note: class C implements A, B
Example of Multiple Inheritance in Java by Interface
Input Value
Example of Multiple Inheritance in Java by Interfaceinterface Company{ void print(); } interface Study{ void show(); } class abms implements Company,Study{ public void print(){System.out.println("Basic Engineer");} public void show(){System.out.println("Hello Student");} public static void main(String args[]){ abms obj = new abms(); obj.print(); obj.show(); } }
Let’s run the code
Output Value
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JAVA Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments