Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot create a concrete sub-class of a public abstract class, in another package

 
Rushabh Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags instead of quote tags for source code.
 
Rushabh Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Please use code tags instead of quote tags for source code.


My bad. Fixed it.
 
Rushabh Shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic