Hello Friends,
Knowledge is POWER, oNLY wHEN iT iS aPPLIED....
This blog is created for all those who like Java Programming. Specially for students. You can post your queries, your experiences, also you can post your doubts and share some java related Fundas. This is to enhance our knowledge and skills by sharing.
I will also publish few great Articles across the technology world as well as if any new JOB openings in "Placements" section.
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
hi sir i am NISHITH KUMAR(FROM SEED CHINCHWAD 7.30 AM BATCH)and ve a doubt in following e.g.
ReplyDeleteinterface 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
Dear Nishith,
ReplyDeleteas 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.