| Author |
Cannot create a concrete sub-class of a public abstract class, in another package
|
Rushabh Shah
Greenhorn
Joined: Mar 29, 2004
Posts: 18
|
|
While trying out some stuff for examination, came across a scenario which defeats the public keyword to a certain extent. My 2 cents, check out the following code
Well not sure how everybody would have guessed it but for the first time i wud hv thot it prints Hi. But actually the code doesn't compile, as the abstract method is default.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Yeah even though the class is visible(can extend or create instances), the member method is not visible outside the package. Also even if it was visible(public or protected), your code will not compile as you have not overridden the abstract method.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
|
Please use code tags instead of quote tags for source code.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Rushabh Shah
Greenhorn
Joined: Mar 29, 2004
Posts: 18
|
|
Jesper Young wrote:Please use code tags instead of quote tags for source code.
My bad. Fixed it.
|
 |
Rushabh Shah
Greenhorn
Joined: Mar 29, 2004
Posts: 18
|
|
Nitish Bangera wrote:Yeah even though the class is visible(can extend or create instances), the member method is not visible outside the package. Also even if it was visible(public or protected), your code will not compile as you have not overridden the abstract method.
Nitish my bad, about me() method was not intended. Have rectified & put up the original code. Just that used me() for testing locally
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Still it won't work as you are making the overridden method access modifier more restrictive(from protected or public to default)
|
 |
Rushabh Shah
Greenhorn
Joined: Mar 29, 2004
Posts: 18
|
|
Nitish Bangera wrote:Still it won't work as you are making the overridden method access modifier more restrictive(from protected or public to default)
not really... its default only... like the one in parent
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
If its default, then its not visible to the class outside the package. You are creating a new method here for the subclass. I suppose this will be running fine now.
|
 |
 |
|
|
subject: Cannot create a concrete sub-class of a public abstract class, in another package
|
|
|