"extends = implementation inheritance
implements = interface
inheritance"
In Java, the above description isn't complete. An
interface can "extend" another interface, and that is "interface inheritance".
A class can "implement" an interface, and that is also "interface
inheritance".
When a class A "extends" another class B, that is actually
BOTH interface inheritance AND implementation inheritance. Java doesn't let you
have implementation inheritance without also publically inheriting the
interface; some other languages do.
Note that "implementation inheritance"
is for code reuse, and sometimes for encapsulation purposes (A inherits the
implementation of B). "interface inheritance" expresses the subtyping
relationship where A is a subtype of B, in other words, it expresses that A
"is-a" B, and can be used in places where a B is expected. E.g. anywhere you
have a "foo" which you expect to be of type B, it would be reasonable to have an
instance of the derived type A (the subtype) instead. This is a feature of "the
Java type system".
(Every programming language formally has a type system,
which is an important mathematical abstraction that is embodied in the language
semantics. It describes what types a value can have, and the conversions or
substitutions that are possible between those types.) [ Reply to This | Parent | # ]
|