Hi krishna,
class Me {
public void jump() {
// Codes and more codes
}
}
class You {
public void jump() {
// Codes and more codes
}
class They {
public void jump() {
// Codes and more codes
}
}
Ok, now I want to notify all the instances of the above classes
when some interesting events happens
in some other object (call it "X"). When "X" jumps, objects of class
"Me", "You", "They" needs to notifed, that is i want 2 call the
jump() method in those objects.
[ Later u may want to create few more classes, that needs to be notified. ]
How do i go about the design ? How do the class "X" calls the
jump() method of these objects ?. [ assuming an instance of class "X" has
reference to the above instances, in a Collection (some how i managed it !! ) ]
So i need to say
" class You extends Me " ??? // Assuming am not a human !!
and
" class They extends You " ?? // They r humans !! maybe ..
NOOOO.
I don't inherit from some meaningless class just to reuse some codes.
But the above classes have something in common, they are interested to
know when "X" jumps, thats all in common.
So wat do i do ??
I will take that "commonality" in those classes and make an "interface" !!.
interface JumpingListener {
public void jump();
}
and i modify the classes to say that they are jumping listeners!!.
class Me implements JumpingListener {
public void jump() {
// implementing the method in my own way.
}
}
class You implements JumpingListener {
public void jump() {
// implementing the method in Your own way.
}
class They implements JumpingListener {
public void jump() {
// implementing the method the way They like.
}
}
Now all these classes have something in common, they r jumping listeners.
Now some skeleton of class X
class X {
Collection l;
public void addJumpingListener(JumpingListener jl) {
// add to to collection
}
// X jumps here ( the event )
// listeners gets notifed here [ the listeners are taken from the collection
// and their jump() method is called.
}
* X need not know the exact "class" of the object, ("Me", "You".. and some classes
not yet invented)all it want to know is the type of the object, in this case
these objects will be of type "JumpingListener". So "X" deals with the
JumpingListener types.
* In future you can create many more objects of type "JumpingListener",
class "X" need not know about them.
( these r just the few things abt the interfaces, and u will really appreciate the interfaces when u c the Java APIs)
So r "interface" good enough ??? Convinced ?
OK atleast u got bored !.
---------
Jon Aryan
---------
Originally posted by psr krishna:
hi all
Still iam not convinced why because?
the real use of inheritance is when your really inheriting one or more defined properties from parent class or interface.But in case of interface the property(method) is (are) completely not implemented,So instead of inheriting that un implemnted methods from interface i can easily give complete implementation for those methods.We can take the case of EVENT LISTENER INTERFACES
For example I can simply write addActionPerformed() and etc., methods with complet implementation as per my need without inhereting from ActionListener interface then it will work.
So now my point is better clear now if iam wrong please pardon .
--thanks
sivarm
[This message has been edited by Jon Aryan (edited October 11, 2000).]