Total Pageviews

Sunday, September 30, 2012

INTERFACE

CAN A CLASS IMPLEMENT 2 INTERFACES HAVING COMMON METHOD NAME??? 

We know that a class can extend only one class(since multiple inheritance is not supported) and implement any number of interfaces. Consider a situation where a class implements 2 interfaces and both the interface have one method in common. 

Consider an eg:


interface inter1
{
    void print();
}
interface inter2
{
    void print();
    void increment();
}
public class psvm implements inter1,inter2
{
    int test=10;
    
    @Override        //OPTIONAL
    public void print()
    {
        System.out.println("hai in print()");
    }
    
    public void increment()
    {
        System.out.println(++test);
    }

    void other_methods()
     {
         //Method Body
      }
    public static void main(String args[])
    {
        System.out.println("hai");
        psvm object=new psvm();
        object.print();
        object.increment();
    }
}

this is possible... the implementing class will have the method common for two interfaces which will satisfy the requirements of both the interfaces(requirements in the sense, if an interface is implemented by a class, then the class must use all the methods given in the interface). Hence it doesn't matter what type of reference variable is used to call the common method. 

To make my statement sooo elementary, let me tell you like this. You promise your friend Thomas that you'll wear red shirt tomorrow. You also promise your friend Jack that you'll wear red shirt. Is it necessary to put two red shirts??? !!!! :-D One red shirt will satisfy the promise made to your friends. The same is applied here.

And note that void print() is prefixed with the keyword public. This is needed to distinguish the print() method from the other normal methods (eg: void other_methods() ) of the class. The keyword public indicates the compiler that the function print() has some declaration before its use (here it indicates that print() is in the interface). Hence public void print() is used rather than mere void print().

Also it is not necessary that all the methods in the interface must be used once a class implements an interface. It may not use some of the methods specified in the interface. In such case, the class name must be prefixed with the keyword abstract.

For eg.
Suppose if we develop a class named xxx which implements inter2 and if class xxx uses only the print() method, then it must be written as
abstract class xxx implements inter2
rather than
class xxx implements inter2This kind of implementation of interface is often called as partial implementations.

And finally, since interface does not contain definition of the methods, an interface can't implement another interface but it can extend other interfaces. It can extend multiple interfaces too.
Eg:
interface A
{
          method1();
          method2();
}
interface B
{
          method3();
}
interface C extends A,B
{
           method4();
}

This is the speciality of an interface, since classes can't extend more than one class but interface can extend one or more interfaces... ;-)

Thanks for reading my blog :-) positive comments are invited. Stay tuned for my next blog... ;-)

2 comments:

  1. dai one doubt da....the KEYWORD u used is "extends" to inherit interface a and b to c,actu we must use "implements" na ????????

    ReplyDelete
  2. Nope!!! this is the twist... :D an interface can't implement another interface... i've given the explanation above the last example... SINCE INTERFACE DOES NOT CONTAIN DEFINITION OF THE METHODS, AN INTERFACE CAN'T IMPLEMENT ANOTHER INTERFACE BUT IT CAN EXTEND OTHER INTERFACES
    ;-) Hope nw u got the point... :)

    ReplyDelete