Wednesday, 16 December 2015

What is interface ? and example ?

Interface:

An interface is a reference type in java, it is similar to class.
Interface is contains "abstract methods." and "static constants" methods.
The interface in java is a mechanism to achieve "fully abstraction" and "multiple inheritance".
You cannot instantiate an interface
An interface does not contain any constructors.
An interface is not extended by a class; it is implemented by a class
An interface can extend multiple interfaces.
implements keyword is used by classes to implement an interface.
Interface cannot be declared as private, protected or transient.
All the interface methods are by default abstract and public.
+++++++++

 package oops;

interface I
{void print();
}

public class CI implements I {

    public static void main(String[] args) {
        CI c = new CI();
        c.print();

    }

    @Override
    public void print() {
        System.out.println("Printed");
      
    }

}


+++++++++++++++++++
Sample code:
//Interface example

    interface inter
    {
        static void method1()
        {
            System.out.println("am in method 1:");
        }
       
    }
    public class Interface1 implements inter
    {
    void method1()
    {
        System.out.println("am in method 2:");
    }
    void method3()
    {
        System.out.println("am in method 3:");
    }
   
    public static void main(String[] args)
    {
        Interface1 obj=new Interface1();
        obj.method1();       
    }
}

Output: am in method 2:

No comments:

Post a Comment