- The member variables inside interface must be static or final
- The member functions inside interface should declared only not defined, means it just shows method’s return type and parameters only.The body of it means how that method works throughout program must not be defined
Also read:- Exception handling in java
Syntax is just one step ahead
interface interface_name{
final/static variable_name;
return_type function_name(parameters);
}
Interface provides platform for other class to implement the function declared inside interface.Interface uses extends and implements both keyword.
When we declares the function inside interface it is necessary to implement that function inside class.To implement it there must be class
class class_name implements interface_name
implements is the keyword that is used for implementation process of interface inside particular class.. Suppose we have class B and interface is A like below
interface A{
void sum(int a,int b);
}
class B implements A{
public void sum(int i,int j)
{
System.out.println(“Sum : “+(i+j));
}
- The main point to consider here is that the function which you implement inside class must have public access property.
Interface can be implemented for multiple class with different function body.One class can also implement multiple interface .So the concept of multiple inheritance which is not possible directly in inheritance is possible with the help of interface. Interface can also have property of extends! It means that one interface can extends other interface .Suppose we have two interface
like below
interface A{
…
…
…
}
interface B extends A{
…
…
…
}
Here we can see that extends keyword is used between two interface .
- Another main point to keep in mind that The functions inside interface must be implemented by class.If the functions of interface are not implemented inside the class then that class must be abstract.Abstract class is the class in java which behaves same like interface but the functions and member variables behaves different from interface.
You will get better idea about interface by executing below java program
Code
interface a //Interface a
{
void math1(); //first method
void math2(); //second method
}
class b implements a //implement interface
{
public void math1() //Implementation of first function and access is public
{
System.out.println(“Math1 is called”);
}
public void math2() //Implementation of second function and access is public
{
System.out.println(“Math2 is called”);
}
}
class interdemo //main class
{
public static void main(String args[]){
b bb=new b();
bb.math1(); //calling both functions
bb.math2();
}
}
Output
Math1 is called
Math2 is called
Logic
Here we have first declared interface .Inside there we have declared two method.Then class implements interface and define that two methods with public access .
With the help of object ,methods are called..So this is simple program of interface which will help you to get exact idea about it’s concept.
Also read:- Develop simple GUI application in java
Multiple inheritance is possible with the help of interface.By executing below program you will get idea about it
Code
interface a{
void math1();
}
interface b extends a //interface extends interface
{
void math2();
}
class c implements b //class implements interface
{
public void math1()
{
System.out.println(“A class implementation”);
}
public void math2()
{
System.out.println(“B class implementation”);
}
void math3()
{
System.out.println(“C class method”);
}
}
class d extends c //class extends class
{
void math4()
{
System.out.println(“D class method”);
}
}
class intermulti{
public static void main(String args[]){
d obj=new d();
obj.math1();
obj.math2();
obj.math3();
obj.math4();
}
}
Output
a class implementation
B class implementation
C class method
D class method
Logic
Here we have two interface and two class.First interface have one method which is extended by second interface.Now C class implements B interface.So indirectly it also implements A interface method.Now last class D extends C class .So this is sample of multiple inheritance with using interface.
Also read:- Simple text based game in java
So this is the complete tutorial..With help of this you will understand about interface in java.Hope this will help you!!