Thursday, January 22, 2015

Java Interface- Basics

 Interface
Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.
 They are used for abstraction. Since methods in interfaces does not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes.

Declaration
Interfaces are declared by specifying a keyword “interface”. E.g.:

package com.vikas;
public interface MyInterface {
    public void Method1();
    public void Method2();
    int x=5 ;   
    int y; // Compiler will throw error
}


Also-

package com.vikas;
public interface MyInterface1 {
    public void Method3();
    int x=10;
}
Interface Implementation
It has to provide the body of all the methods that are declared in interface.
Note: Class implements interface but an interface extends another interface.

For above two interface- MyInterface and MyInterface1 implementation class will be:

package com.vikas;
public class MyImplmnt implements MyInterface,MyInterface1{
    public void Method1(){
        System.out.println("My first name is Vikas");
    }
    public static void main(String[] args) {
        MyInterface obj = new MyImplmnt();
        MyInterface1 obj1 = new MyImplmnt();
        obj.Method1();
        obj.Method2();
        obj1.Method3();
       
    }
    @Override
    public void Method2() {
        //int x;
        //x=10;
        System.out.println("My last name is Kumar");
        System.out.println("value--> "+MyInterface1.x);
    }
    @Override
    public void Method3() {
        System.out.println("I am QA Engineer");
       
    }
}

OUTPUT:

My first name is Vikas
My last name is Kumar
value--> 10
I am QA Engineer

Key Points:

1. We can’t instantiate an interface in java.
2. implements keyword is used by classes to implement an interface.
3. While providing implementation in class of any method of an interface, it needs to be mentioned as public.
4. Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.
5. All the interface methods are by default abstract and public.
6. Variables declared in interface are public, static and final by default.
7. Interface variables must be initialized at the time of declaration otherwise compiler will through an error.
8. Any interface can extend any other interface but cannot implement it. Class implements interface and interface extends interface.
9. Variable names conflicts can be resolved by interface name(Refer the code above-> MyInterface1.x)

A class can extend only one class but can implement any number of interfaces. It saves you from Deadly Diamond of Death(DDD) problem.

 

No comments:

Post a Comment