Page 404 - CITS - Computer Software Application -TT
P. 404

COMPUTER SOFTWARE APPLICATION - CITS




           As you can see in the above example, both methods m1() and m2() defined in interface are declared with no
           body and do not have public or abstract modifiers present. The variable x declared in MyInterface is like a simple
           variable.
           Java compiler automatically adds public and abstract keywords before to all interface methods. Moreover, it also
           adds public, static, and final keywords before interface variables. Look at the below figure to understand better.
























           Note:

           a   Earlier to Java 8, an interface could not define any implementation whatsoever. An interface can only declare
              abstract methods.
           b   Java 8 changed this rule. From Java 8 onwards, it is also possible to add a default implementation to an
              interface method.
           c  To  support lambda functions,  Java 8  has added a  new feature to  interface. We  can also declare default
              methods and static methods inside interfaces.
           d   From Java 9 onwards, an interface can also declare private methods.
           Features of Interface
           There are the following features of an interface in Java. They are as follows:
           1  Interface provides pure abstraction in Java. It also represents the Is-A relationship.

           2   It can contain three types of methods: abstract, default, and static methods.
           3   All the (non-default) methods declared in the interface are by default abstract and public. So, there is no need
              to write abstract or public modifiers before them.

           4   The fields (data members) declared in an interface are by default public, static, and final. Therefore, they are
              just public constants. So, we cannot change their value by implementing class once they are initialized.
           5   Interface cannot have constructors.
           Extending Interface in Java with Example

           Like classes, an interface can also extend another interface. This means that an interface can be sub interfaces
           from other interfaces.
           The new sub-interface will inherit all members of the super interface similar to subclasses. It can be done by using
           the keyword “extends”. It has the following general form:
           Syntax:
           interface interfaceName2 extends interfaceName1
           {
           // body of interfaceName2.

             }


                                                           391

 CITS : IT&ITES - Computer Software Application - Lesson 109 - 115  CITS : IT&ITES - Computer Software Application - Lesson 109 - 115
   399   400   401   402   403   404   405   406   407   408   409