Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Abstract method query

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we override an abstract method with default accessibility being declared in a public(abstract) class and in a package different than that of its subclass?

Here's the code

A compile-time error is what I get..
Sub.java:2: Sub is not abstract and does not override abstract method print() in A.Abstract

 
Saloon Keeper
Posts: 12002
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The subclass has to be in the same package, or the abstract method has to be declared protected.
 
Rahul Saple
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought so too. But then I thought there must be a way out since the compiler doesn't flag such a use. Thanks anyway.
 
Stephan van Hulst
Saloon Keeper
Posts: 12002
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would the compiler flag? It's perfectly valid to declare a package private abstract method in a public abstract class. In this case, implementations are restricted to the package, but the interface that the abstract class defines is still usable outside the package.

[edit]

Oh, I suppose you're referring to the confusing error message the compiler gives you now. Yeah, but it probably would have been marginally useful for the designers to make the compiler print a special error message just for this specific case.
 
Rahul Saple
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I certainly did pounce on the poor compiler to help me out on this one(not the first time). Guess I am the dumb one here.
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic