Interface in java

Rules of interface

* An interface is created by using a Keyword interface and its name.
Ex: interface Animal {   }
* we cannot create object of interface but we can create reference variables just like abstract class.
* interface only contents abstract methods.
* to use interfaces in any class you need to use a keyboard implements and interface name.
Ex: class Dog implements Animal{   }

For methods

The methods used in interface must be public and abstract.
So we may or may not declare abstract and public before the method name.

For variables

It is better to avoid using field variables if used they should be constants.
They are public static and final.

Practical example

Post a Comment

0 Comments