Java – Implementing interfaces

Java – Implementing interfaces 2017-08-09T12:11:32+00:00

Java Implementing interfaces :

Once an interface has been defined, one or more classes can implement that interface.

To implement an interface, include the implements clause in a class definition, and then create the methods declared by the interface.

The general form of a class that includes the implements clause looks like this:

Access-specifier class classname [extends superclass] [implements interface, [, interface..]]

{

// class body

}

If a class implements from more than one interface, names are separated by comma.

If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface.

The methods that implement an interface must be declared as public.

The type-signature of implementing method must match exactly the type signature specified in the interface.


 


Output :
We greet – ABCD
We pray at Amritsar XYZ
EX 2 :

 


Output :
This is display of i1
This is display of i2
This is display of i1
This is display of i2
EX 3 : // Implementing interface having common function name

 

Output :
This is display ..

Note : When implementing an interface method, it must be declared as public. It is possible for classes that implement interfaces to define additional members of their own.

Partial Implementation of Interface :

If we want to implement an interface in a class we have to implement all the methods defined in the interface.

But if a class implements an interface but does not fully implement the method defined by that interface, then that class must be declared as abstract.

EX :


 


Output :
This is display of 1
This is display of 2

Prev Next