What is the interface in java?
An interface in java , like an abstract class can be used to achieve run time polymorphism (dynamic method dispatch). Following are
important point we must remember with respect to an interface:-
1) Just like a class an interface also
can contain data but all the data members declared is an interface are by
default public static and final.
2) Just like a class an interface also
can contains method but every method declared as an public and abstract.
3) Just like a class inherits another
class it can also inherit an interface but the keyword use in inheritance for
interface is implements.
4) Just like an abstract class we can
create reference of an interface but we can never create an object. However we can
creates object of child class of an interface and assign it to the reference of
the interface and using that reference we can call those method of the child class
which it has inherited and overridden from the interface.
5) Whenever the class implement the
interface then it is compulsory to override every abstract method inherited
from the interface If the child class falls to do so then java will force the
programmer to declare the child class as abstract and not allow Instanciation.
6) Although a class extends one class
but can implement multiple interface thus we can says that interface is
alternate to the multiple inheritance in java.
SYNTAX OF DECLARING INTERFACE:
Interface <interface_name>
{
public static final <data_type>
<variable_name>=val;
\\ public static final is automatically prefixed by
java.
public abstract <return_type>
<method_name> (args);
\\ public abstract is automatically prefixed by java.
}
.
Comments
Post a Comment