Interface -


Interface is similar to a class that is a collection of methods and variables. But in interface methods are public abstract and variables are public static final by default.
That is interface methods does not contain any body because they are abstract and variables need to be initialized and cannot be changed their after because they are final.
Once an interface is defined any number of classes can implement it. Also a class can implement more than one interface.
Java supports the concept of multiple and hybrid inheritance through the use of interface.

Interface Declaration -

An interface is declared very similar to a class declaration. The general form of interface declaration is as follows -
access_specifier  interface Interface_Name
{
    variables declarations;
    method declarations;
 }

Here access_specifier can be any valid java access specifier and if no access specifier is included, then it will be default access specifier i.e. interface can be access by other members of the package in which it is declared.
Access specifier is followed by keyword interface that is used to declare an interface.
And it is followed by Interface_Name and it can be any valid Java identifier.
Variables declared in an interface will be public static and final implicitly weather you declare them as public static final or not. Similarly methods will be public abstract implicitly.

Here is an example of interface that defines one variable and two methods -

interface Ia
{
    int MIN = 1;            // It will be public static and final.
    int factorial(int n);  // both the methods will be public and abstract.
    void display();
}                    
Note that methods do not have any body because they are abstract and the class that will implement this interface will define the code for these methods.

Implementing the Interface -

Once an interface is declared, it can be implemented by any number of classes as follows -
class Class_Name implements Interface_Name
{
    //Class body
} 
A class uses the keyword implements to implement any interface.
Note - While implementing the methods of interface you should declare them public otherwise an error will be shown. This is because, in interface methods are by default public so while overriding you cannot decrease the scope.

A more general form of interface implementation may be as follows -

Class Class_Name extends Superclass_Name implements Interfcae1_Name, Interfcae2_Name....
{
    //Class body
} 

        
A class may implements any number of interfaces and they are separated by commas. A class implementing interfaces can also inherit super class as shown above.

Example -

interface Ia
{
    int MIN = 1;
    int factorial(int n);
    void display(int n);
}

class Test implements Ia
{
    public int factorial(int n)
    {
        int ans = 1;
        for(int i=1;i<=n;i++)
        {
            ans = ans*i;
        }
        return ans;
    }
    
    public void display(int n)
    {
        System.out.println(n);
    }
    
    public void info()
    {
        System.out.println("Example of Interface implementation");
    }
}
public class Call {

    public static void main(String[] args) {

        Test t1  = new Test();
        int f = t1.factorial(5);
        System.out.println("Factorial is :" + f);
        t1.display(2);
        t1.info();  
       System.out.println("Value of variable :"+t1.MIN);  // or Test.MIN, because MIN is static
    }
       
}

Output -

Factorial is :120
2
Example of Interface implementation
Value of variable: 1
Here the class Test is implementing the interface Ia. Note that class Test implements all the methods of interface Ia as well as it also define its own method. So a class can also define its own methods in addition to interface methods.

Using interface references -

We can also receive objects of a class in interface reference that is implementing the interface. Because that interface will work as its parent and can receive any objects of its child's.
Always remember that when we are receiving objects in interfaces, we can only access methods and variables that are defined in that particular interface, we cannot access any variables or methods defined in the class directly because interface does not have any information about those members.
However there is a way to access those methods too by type casting interface reference into class reference. Consider the following example -
interface Ia
{
    int MIN = 1;
    int factorial(int n);
    void display(int n);
}

class Test implements Ia
{
    public int factorial(int n)
    {
        int ans = 1;
        for(int i=1;i<=n;i++)
        {
            ans = ans*i;
        }
        return ans;
    }
    
    public void display(int n)
    {
        System.out.println(n);
    }
    
    public void info()
    {
        System.out.println("Example of Interface implementation");
    }
}
public class Call {

    public static void main(String[] args) {

        Test t1  = new Test();
        Ia ref = t1;      //  Or Ia ref = new Test();
        int f = ref.factorial(5);
        System.out.println("Factorial is :"+ f);
        ref.display(2);
        //ref.info();    Error because not a member of Ia
        Test t = (Test)ref;  //type casting ref to Test
        t.info();
    }
}
            
Here we have created a reference ref of interface Ia that contain object t1 of Test class. So we can access all methods and variables of Interface Ia using this reference, But to access that of class Test we have to type cast this back to Test.

Extending Interfaces -

Like classes, interfaces cab also be inherited, But an Interface can inherit any number of interfaces whereas a class can inherit only single class. When an interface inherits any interface then all the members of parent interface will be available in that interface.

Example -

interface Ia
{
    int MIN = 1;
    int factorial(int n);
    void display(int n);
}

interface Ib
{
    int MAX = 1;
    int square(int n);
    void display(int n);
}

interface I extends Ia, Ib
{
    void details();
}

Here interface I is extending the interfaces Ia and Ib, i.e all the members of Ia and Ib are available in interface I. So if any class will implement the interface I, then it has to implements all the methods of Ia, Ib and I.

Implementing Multiple Interfaces -

A class can implement any number of interfaces as required. An d in that case class need to implement all the methods available in those interfaces.

Example -

interface Ia
{
    int MIN = 1;
    int factorial(int n);
    void display(int n);
}

interface Ib
{
    int MAX = 1;
    int square(int n);
    void display(int n);
}

interface I extends Ia, Ib
{
    void details();
}

class Test implements I, Ia, Ib
{
    public int factorial(int n)
    {
        int ans = 1;
        for(int i=1;i<=n;i++)
        {
            ans = ans*i;
        }
        return ans;
    }
    
    public void display(int n)
    {
        System.out.println(n);
    }
    
    public int square(int n)
    {
        return n*n;
    }
    public void details()
    {
        System.out.println("Just a Demo");
    }
    public void info()
    {
        System.out.println("Example of Interface implementation");
    }
}

public class Call {

    public static void main(String[] args) {

        Test t1 = new Test();
        System.out.println("Factorial is : "+t1.factorial(5)); 
        System.out.println("Squar is : "+t1.square(5));
        t1.details();
        t1.info();
        I ref = t1;  // can call all members of I, Ia and Ib
        Ia refA = t1;  // can call only Ia members
        Ib refB = t1;  // can call only Ib members
    }
}

    
Here class Test implementing three interfaces Ia, Ib and I. So it has to implements all the methods available in these interfaces.
As we already know that if a class that is implementing any interface then those interfaces will work as their parent. So here Ia, Ib and I wil be parent of Test class and ,so we can receive object of Test class in references of Ia, Ib and I.
Note -
While working with interfaces always remember the following points -

1- class extends class( only one).
2- interface extends interface (any).
3- class implements interface (any).