02 July, 2012

A Moment with Marker Interface in Java Development, What is Marker Interface

Under editing..........

What is Interface ? The face value of Interface.

As there are ample of technology in the area of Computer science, that has many contribution to mankind.When I was a student of computer science, I had many doubts about my future .On which platform or language I should work ? What will be my profession? Out of all I choose Java as soul of my profession.

Rally java has many features and scopes.But when I go for the interface ( like an important organ in the java body) , really I can not calculate the face value of interface.How much essential it is for java followers?The face value of interface is undefined.As we know that interface makes java popular.

The Term  "Interface"

And it is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies inside interface. Interfaces cannot be instantiated and interfaces can only be implemented by classes or extended by other interfaces.Its a protocol of communication between objects.

How to Define an Interface ?

It is quite simple to define an interface. The modifier 'interface' is used to define an interface.The naming convention shall be follows like declaring a class.A class can implement more than one interface separated by comma.And also an interface can extends more than one interface separated by comma.And it contains only signature of the method , no implementation.

public interface SymbolInterface extends int_face1, int_face2, int_face3,int_face4 {
//Declare your member here
}

OR


public class ImplementSymbolInterface implements SymbolInterface,OtherInterfaces {
//Declare your member here
}

It is recommended to make your interface public, so that it can be used by other packages, or it can only visible to that implemented class.And you quite sure that the body of the  interface is always having any number method (without method body) or closing with semicolon.Those method has no implementation inside that interface. For implementing that you may need implementation class.

Except, method an interface can contain constant declaration with few modifiers public,static and final. These method & constant declarations are completely optional, i.e. you can declare an interface without any method or any constants. And when an interface does not contain any method or constants that interface is called Marker Interface.

Note:- Yes, Marker interface is good question asked by major MNC .You can find more details about marker interface from the post A Moment with with Marker Interface in Java Development, What is Marker Interface.

How to Implement an Interface ?

Yes, this is the point that you need in your real life program. Before going to implement you have some brief idea about the term interface & its usage.

Use, implements keyword for implement the interface over a class . Follow few code below :-

Declare an interface :

public interface InterfaceSymbol {

    public void symbolAdd();
    public void symbolDiv();
    public void symbolMul();
    public void symbolSub();
   
}

Implement the above interface for a class :

public class SymbolImplementa implements InterfaceSymbol{

    public void symbolAdd(){
        System.out.println("I am In Add");
    }
    public void symbolSub(){
        System.out.println("I am In Sub");
        }
    public void symbolMul(){
        System.out.println("I am In Mul");
    }
    public void symbolDiv(){
        System.out.println("I am In Div");
    }
}

As per the rule you must have to override the methods of interface in the implementation class. But , remember it is not mandatory , you can avoid it by using :

1--- Adapter Class
2--- Make that implementation class as Final. ( Final is a keyword)

Here I am not describing how to avoid for overriding all methods of interface.This is beyond of this post.You follow other posts related to interface.

Also , like a class one interface can extend ( defined keyword) another interface.And the behavior of that interface will remains same.When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

This is main function where the implementation is done.

public  class MainImplementationClass  extends SymbolImplementa implements InterfaceSymbol{

    /**
     * @param args
     */
    public void symbolAdd(){
        System.out.println("inside ");
    }
    public static void main(String[] args) {
        testf fobj=new testf();
        SymbolImplementa impobj;
        //fobj.symbolAdd();
        impobj=fobj;
        impobj.symbolAdd();
        System.out.println("I am in Main.........");
       
    }

}



The word of Caution.

Then you need to rewrite an interface, be careful about the pitfall. In the above example  InterfaceSymbol interface is implemented by the class SymbolImplementa , but  If you make few changes in the old interface , all classes that implement the old InterfaceSymbol interface will break because they don't implement the interface anymore. Programmers relying on this interface will oppose deeply.So, enjoy the flavor of interface in java programming.



By Manoj.