aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract method query Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Abstract method query" Watch "Abstract method query" New topic
Author

Abstract method query

Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
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

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3053
    
    1

No. The subclass has to be in the same package, or the abstract method has to be declared protected.
Rahul Saple
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
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
Bartender

Joined: Sep 20, 2010
Posts: 3053
    
    1

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

Joined: Aug 02, 2006
Posts: 46
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Abstract method query
 
Similar Threads
methods that are static AND final
Doubt about final methods
Abstract Class
Overiding super class method to an abstract method
doubt interface, abstract class