Saturday 23 March 2013

Implementing Interfaces


Once an interface has been defined, one or more classes can implement that interface.  “implements” keyword used for implementing the classes.  The syntax of implements is as follows.
            Syntax:            class class-name implements interface1, interface2, .. interfacen
                                    {
                                                ……..
                                                ……..              // class body
                                                ……..
                                    }
            If a class implements more than one interface, the interfaces are separated with a comma operator.  The methods that implement an interface must be declared public.  Also type signature of the implementing method must match exactly the type signature specified in the interface definition.

Example:

interface it1
{
            int x=10,y=20;
            public void add(int a,int b);
            public void sub(int a,int b);
}
class it2 implements it1
{
            public void add(int s,int w)
            {
            System.out.println("Addition="+(s+w));
            }
            public void sub(int s,int w)
            {
            System.out.println("Subtraction="+(s-w));
            }
            public static void main(String[] args)
            {
            it2 obj=new it2();
            obj.add(3,4);
            obj.sub(5,2);
            System.out.println(obj.x+obj.y);
            obj.x=70;        // error since x is final variable in interface
            }
}

Note:
1.         Interface methods are similar to the abstract classes so, that it cannot be instantiated.
2.         Interface methods can also be accessed by the interface reference variable refer to the object of subclass.  The method will be resolved at run time.  This process is similar to the “super class reference to access a subclass object”.

Example:

interface it1
{
            int x=10,y=20;
            public void add(int a,int b);
            public void sub(int a,int b);
}
class it2 implements it1
{
            public void add(int s,int w)
            {
            System.out.println("Addition="+(s+w));
            }
            public void sub(int s,int w)
            {
            System.out.println("Subtraction="+(s-w));
            }
            public static void main(String[] args)
            {
            it2 obj=new it2();
            it1 ref;
            ref=obj;
            System.out.println(ref.x+ref.y);
            }
}

3.         If a class includes an interface but does not fully implement the methods defined by that interface, then the class becomes abstract class and must be declared as abstract in the first line of its class definition.

Example:

interface it1
{
            int x=10,y=20;
            public void add(int a,int b);
            public void sub(int a,int b);
}
abstract class it2 implements it1
{
            public void add(int s,int w)
            {
            System.out.println("Addition="+(s+w));
            }
}
class it3 extends it2
{
            public void sub(int s,int w)
            {
            System.out.println("Subtraction="+(s-w));
            }
            public static void main(String[] args)
            {
            it3 obj=new it3();
            obj.add(5,6);
            }
}

No comments:

Post a Comment