Wednesday 1 February 2017

Interface

Interface

An interface in java is a blueprint of a class. It has static constants and abstract methods only no method body.
Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces.

It is used to achieve fully abstraction and multiple inheritance in Java.
Java Interface also represents IS-A relationship.

There are mainly three reasons to use interface. They are given below.
   1)  It is used to achieve fully abstraction.
   2)  By interface, we can support the functionality of multiple inheritance.
   3)  It can be used to achieve loose coupling.

Point to be remember regarding Interface.
    - The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members
    - You cannot instantiate an interface just like an abstract class.
    - An interface does not contain any constructors.
    - All of the methods in an interface are abstract.
    - An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
    - An interface is not extended by a class; it is implemented by a class.
    - An interface can extend multiple interfaces.

Examples on Interface For Practice.

Program # 1
abstract interface E
{
    abstract void test1();
    void test2();
    public void test3();
}
Note:
1.Abstract keyword before an interface is optional.
2.inside an interface we can keep two type of member 1.constant (2)abstract method
3.we can not keep any concrete method ,SIB,IIB inside interface.
4.By default every member inside an interface are abstract and public . public and abstract are optional.

Program # 2
Note: In this program while implementing interface method use access specifier as 'public'
class F implements E
{
    public void test1()
    {
        System.out.println("Test1");
    }
    public void test2()
    {
        System.out.println("Test2");
    }
    public void test3()
    {
        System.out.println("Test3");
    }
    public static void main(String[] args)
    {
      // E e1=new E(); can not be instantiated.
      E e1=null; //allow
      F f1=new F();
      f1.test1();
      f1.test2();
      f1.test3();
      System.out.println("done");
    }
}

Output
Test1
Test2
Test3
done

Program # 3
interface G
{
    void test1();
    int test2();
    void test3(int i);
}
class H implements G
{
    public void test1()
    {
        System.out.println("Test1");
    }
    public int test2()
    {
        System.out.println("Test2");
        return 10;
    }
    public void test3(int i)
    {
        System.out.println("Test3");
    }
}
class Manager
{
    public static void main(String[] args)
    {
      H f1=new H();
      f1.test1();
      f1.test2();
      f1.test3(90);
      System.out.println("done");
    }
}

Output:
Test1
Test2
Test3
done

Program # 4
Note:One class can implement any number of interfaces,it has to implement every methods of interface.
interface I
{
    void test1();
}
interface J
{
    void test2();
}
class K implements I,J //in K class inherited method must implement otherwise compiler error
{
    public void test1()
    {
        System.out.println("Test1");
    }
    public void test2()
    {
        System.out.println("Test2");
    }
}
class Manager1
{
    public static void main(String[] args)
    {
        K f1=new K();
        f1.test1();
        f1.test2();
        System.out.println("done");
    }
}

Output:
Test1
Test2
done

Program # 5
Note: The keywords Order must be proper first should be extends then implements
interface L
{
    void test1();
}
class M
{
    void test2()
    {
        System.out.println("Test2");
    }
}
class N extends M implements L
//class N implements L extends M
{
    public void test1()
    {
        System.out.println("Test1");
    }
}
class O
{
    public static void main(String[] args)
    {
      M m1=new M();
      m1.test2();
      N f1=new N();
      f1.test1();
      f1.test2();
      System.out.println("done");
    }
}
Output:
Test2
Test1
Test2
done

Program # 6
Note: Method inside interface P and Q both should be implement either in R or S class.
interface P
{
    void test1();
}
interface Q
{
    void test2();
}
class R
{
    public void test1()
    {
        System.out.println("Test1");
    }
}
class S extends R implements P,Q
{
    public void test2()
    {
        System.out.println("Test2");
    }
}
class Manager2
{
    public static void main(String[] args)
    {
      S s1=new S();
      s1.test1();
      s1.test2();
      System.out.println("done");
    }
}
Output:
Test1
Test2
done

Program # 7
Note: we can use interface to interface extends.
interface T
{
    void test1();
}
interface U
{
    void test2();
}
interface V extends T,U
{
    void test3();
}
class W implements V
{
    public void test1()
    {
        System.out.println("Test1");
    }
    public void test2()
    {
    System.out.println("Test2");
    }
    public void test3()
    {
        System.out.println("Test3");
    }
}
class Manager3
{
    public static void main(String[] args)
    {
      W s1=new W();
      s1.test1();
      s1.test2();
      s1.test3();
      System.out.println("done");
    }
}

Output:
Test1
Test2
Test3
done

Program # 8
Note:
1.inside the abstract class or interface if static member is there then static member can be access by class name.
2.every attribute of interface are by default public ,static and final
3.static method can not be abstract because static method can not inheriting in sub class.
4.interface method should not be static ,because further they has to implement.

abstract class X
{
    static void test1()
    {
        System.out.println("Test1");
    }
    static int i=20;
    void test2()
    {
        System.out.println("Test2");
    }
}
interface Y
{
    static int j=30;
}
class Z
{
    public static void main(String[] args)
    {
      X.test1();
      System.out.println(X.i);
      System.out.println(Y.j);
    }
}

Output:
Test1 20 30

Program # 9
interface A
{
}
Output:
compile successfull and generate .class file.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...