|
Authored by: ais523 on Saturday, May 05 2012 @ 01:49 PM EDT |
Classes can inherit from each other, just as in C++. (There are several models
of object orientation floating around, but Java's model of inheritance is pretty
much a simplified version of C++'s.) Meanwhile, package naming has nothing to do
with inheritance at all, nor is there any requirement in theory (or often in
practice) that inheritance respect package boundaries. So in the context of
packages, rather than classes, inheritance does indeed not exist.[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Saturday, May 05 2012 @ 04:16 PM EDT |
He was saying "Inheritance does not exist among packages".
Inheritance is the "is-a" relationship between two classes (and/or
"interfaces", as abstract classes with no implementation in them are
called in Java).
If you have a "class Animal", and you make a "class Cat"
that "extends Animal", then it means that every Cat object (i.e. every
instance of the Cat class) "is-a" Animal. So it's a Cat, but part of
being a Cat is that it "is-a" Animal, so you can think of it, and use
it, as an Animal too.
Inheritance is always a property of a pair of classes. The "base
class" Animal, has a "subclass" Cat. To the Cat class, its
"superclass" is Animal. Any methods or instance variables in the
Animal class are also available in Cat; the programmer writing the Cat class can
also choose to override those methods with new ones, and of course he can add
new Cat-specific methods and instance variables in the Cat class.
Note that "Animal" class and "Cat" class might be in the
same package, or they could be in two different packages--it doesn't matter at
all. "Being in the same package" is only useful for a couple of
things in the Java language; one of them is using the "default" access
control for a method or instance variable (which could be described as
"private access, except that other classes in the same package as this
class have public access". This is not something that ever plays a role in
APIs, its just a convenience feature for implementors.[ Reply to This | Parent | # ]
|
|
|
|
|