File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

subclass

 
ima clark
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a class that is not declared public be subclassed? If so are there any restrictions to this?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either kind can be subclassed.
except if the class is declared final
W.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True enough
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic