Sunday, 11 December 2011


2 comments:

  1. hi sir i am NISHITH KUMAR(FROM SEED CHINCHWAD 7.30 AM BATCH)and ve a doubt in following e.g.

    interface Callback {
    void callback(int param);
    }

    class Client implements Callback {
    // Implement Callback's interface
    public void callback(int p) {
    System.out.println("callback called with " + p);
    }
    }

    class TestIface {
    public static void main(String args[]) {
    Callback c = new Client();
    c.callback(42);
    }
    }
    here it has been created "Callback c"...how is it possible????????
    please tell me with suitable example

    ReplyDelete
  2. Dear Nishith,
    as you said in your mail :
    "i do have a doubt about interfeces .
    as we know dat we can not instantiate interface"

    Let me clear it that interfaces CAN be instantiated.

    It is abstract classes that should NOT be instantiated.

    Now about the example you have given:

    The statement is :

    Callback c = new Client();

    Here 'c' is nothing but a generic reference which is having 'Static reference of type Callback' & 'Dynamic reference of type Client'.

    So when you call c.callback(42); the Dynamic type governs the method selection so Client's

    callback() method will get called.

    Here I am giving one example for your reference:

    As we do in the simple hierarchy of classes : Emp-WageEmp

    so,

    Emp e1 = new WageEmp();

    e1.display();

    in above case Emp can be an interface implemented by WageEmp or a parent class of WageEmp.

    so here, e1.display() will call display method of WageEmp class.



    I hope this answers you doubt.

    Note: Here onwards please post your queries/doubts in the "Queries section" tab created separately.

    ReplyDelete