Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cannot create a concrete sub-class of a public abstract class, in another package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cannot create a concrete sub-class of a public abstract class, in another package" Watch "Cannot create a concrete sub-class of a public abstract class, in another package" New topic
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: 537

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 537

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: 537

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
 
Similar Threads
confuse about protected and static
Can a class extend a class from diff. package?????
Implementing an Interface Down the Inheritance Tree Question
Couple of doubts in Comparator Interface
query on method over riding