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

COMPUTER SOFTWARE APPLICATION - CITS


































           Why do We Use Interface?
           There are mainly five reasons or purposes of using an interface in Java. They are as follows:
           1  In industry, architect-level people create interfaces, and then they are given to developers for writing classes
              by implementing interfaces provided.
           2  Using interfaces is the best way to expose our project’s API to some other projects. In other words, we can
              provide interface methods to the third-party vendors for their implementation.
           For example, HDFC bank can expose methods or interfaces to various shopping carts.
           3  Programmers use interface to customize features of software differently for different objects.
           4  It is used to achieve full abstraction in java.
           5  By using interfaces, we can achieve the functionality of multiple inheritance.

           How to Declare Interface in Java?
           In Java, an interface is declared syntactically much like a class. It is declared by using the keyword interface
           followed by interface name. It has the following general form:

           Syntax:
           accessModifier interface interfaceName
           {
           // declare constant fields.
           // declare methods that abstract by default.

           }
           Before interface keyword, we can specify access modifiers such as public, or default with abstract keyword. Let’s
           understand the declaration of an interface with the help of an example.

           public abstract interface MyInterfac
           {
           int x = 10; // public static final keyword invisibly present.
           void m1(); // public and abstract keywords invisibly present.
           void m2();

           }


                                                           390

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