| Author |
subclass
|
ima clark
Greenhorn
Joined: Apr 23, 2002
Posts: 19
|
|
Can a class that is not declared public be subclassed? If so are there any restrictions to this?
|
 |
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
|
|
|
Assuming we're not talking about inner classes, a class declaration may only have public or "default" accessibility. Either kind can be subclassed.
|
 |
ima clark
Greenhorn
Joined: Apr 23, 2002
Posts: 19
|
|
thanks
|
 |
Wilfried LAURENT
Ranch Hand
Joined: Jul 13, 2001
Posts: 269
|
|
Either kind can be subclassed.
except if the class is declared final W.
|
 |
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
|
|
True enough
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Can a class that is not declared public be subclassed? As Anthony said, yes. If so are there any restrictions to this? Wilfried already pointed out one restriction. Another important one to be aware of concerns the availability (the scope) of the class. If the class is in some package, and the class is declared to be public, then anybody with access to that package can subclass the class. If the class is given default (package) scope, then only classes that are of the same package have access to the class in order to subclass it. For example, this classcan be subclassed by any class that has access to the package dirk. This classwill compile and run just fine (assuming that the compiler is told where to find the package dirk). If the super class were declared with package level access only:the compiler would complain when trying to compile SubClass because dirk.SuperClass is not public in dirk; cannot be accessed from outside package. If the subclass were defined to belong to the dirk packagethen everybody would be happily compiled and executed again (if one can be happy when executed).
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
 |
|
|
subject: subclass
|
|
|