Re: Difference between "extends" and "implements" ?
On 11/30/2012 3:52 PM, itsfaixan@gmail.com wrote:
What is the difference between "extends" and "implements" ?
A class "extends" exactly one other class, its direct
superclass.[*] In `class Sub extends Super {...}', each Sub
instance *is a* Super as well. Also, Sub inherits as much
of Super's implementation as Super allows it to access, so
if the Super class implements a `void eatTwinkies()' method,
Sub gets that same implementation without any effort. (Sub
may choose to supply its own implementation of eatTwinkies(),
but doesn't have to: If it does, that's "overriding.")
[*] Special case: The class java.lang.Object has no
superclass, and all inheritance chains eventually lead upward
to Object. Thus, every object *is an* Object instance.
A class "implements" any number of interfaces. An interface
specifies a group of methods, but does not implement them at
all. The implementing class promises to provide implementations
that match the interface specifications. So if you have
`class Sub extends Super implements Mortal {...}' and the
Mortal interface calls for a `void die()' method, Sub must
implement of `void die()'.
There's a little more to it than that, but not a whole lot
more: There are "abstract" classes, and there are interfaces
that serve only to define constants (a practice now widely
frowned upon, but still found in older API's). Don't worry
about these until you get to a later lesson: For now, work on
understanding the difference between "class" and "interface".
--
Eric Sosman
esosman@comcast-dot-net.invalid