Wednesday, May 29, 2013

Interface In Java (Pure Abstract Class)



Interface:

·         Interfaces are basically used to obtain the concept of multiple inheritances in Java.
·         An interface is a construct which contain purely undefined methods or an interface is a collection of Abstract methods.
·         Syntax:
interface InterfaceName
{
            Variable declarations;
            Method declarations;
}
For example:
interface I1
{
            int x = 10; //public static final int x = 10;
            void add(); //public abstract void add();
            void sub(); //
}

  • When this interface is compiled we get an file with .class (I1.class) extension as similar from the compilation of any class.

  • By default all the variables/data members in the interface are public, final & static and all the methods are public abstract.

1)      An interface is a keyword which is used for creating or developing undefined data types.

2)      <InterfaceName> interface name represent java valid variable name which is treated as name of the interface.
Interface names are used for creating objects indirectly. i.e an object of interface can’t be created directly since it contain abstract methods.

3)      Variable declarations represent type of data members which we use as a part of interface.

4)   All the variables in the interface must be initialized. All the data members of the interface belong to the public static final XXX data members by default.

5)   Here XXX represents data-type, varName and its value. Hence all the data members of the interface are reusable general purpose data members.

6)   Method declaration represents type of methods which we use as a part of interface to perform some general purpose operation.

7)   All methods in an interface are abstract method by default, so we don’t need to use a keyword “abstract” to make them abstract. All the methods of interface are by default reusable i.e. they are “public” by default.
Note:
1)      Whenever we compile an interface we will get .class as an extension for that interface.
2)      All the interfaces are further reusable by the classes.
3)      Interfaces are not final.

Sample Program

interface Operations
{
void add();
}

class IntOperations implements Operations
{
            int a,b,c;
            void add()
            {
                        a = 10;
                        b = 20;
                        c = a+ b;
                        System.out.println (“Sum of the integers : ”, +c);
            }
};

class FloatOperations implements Operations
{
            float f1,f2,f3;
void add()
{
                        f1 = 1.25f;
                        f2 = 3.75f;
                        f3 = f1 + f2;
                        System.out.println (“Sum of floats : ”, +f3);
            }
};

class InterfaceDemo
{
            public static void main(String args[ ])
            {
                        Operations op;
                        op = new IntOperations();
                        op.add();
                       
                        op = new FloatOperations();
                        op.add();
            }
}
Hence object of the interface “Operations” will not be formed because it is abstract and if we create object of the derived class then dynamic binding fails.

Practical implementation of interfaces can be seen the working of ATM banking.
If we have HSBC ATM card, if we use SBI ATM with HSBC ATM card then money will be withdrawn on the behalf of the HSBC ATM.
And this complete operation is done through interfaces.








No comments:

Post a Comment