wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes why private and abstract connot go together? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why private and abstract connot go together?" Watch "why private and abstract connot go together?" New topic
Author

why private and abstract connot go together?

Moha Shaf
Ranch Hand

Joined: Aug 30, 2008
Posts: 30
Why a method cannot be marked both abstract and private?
SCJP 5 Study Guide by Kathy and Bates(Page 45) says that its because the child class cannot even see the private abstract method declared in Parent class.
But the compiler complains even if i declare a default abstract method in Parent class and dont implement it in the child class defined in another package (i.e. The child class defined in another class knows about the default method declared in another package).
Any convincing reason? Or is it just a basic rule in Java?
david john
Greenhorn

Joined: Oct 16, 2008
Posts: 16
A class having even a single abstract method has to be marked as an abstract class.

By JAVA rule first concrete class(Child) has to declare all the abstract method of the abstract class(Parent). Hence, compiler is restricting throwing error.
Moha Shaf
Ranch Hand

Joined: Aug 30, 2008
Posts: 30
Thanks for your reply.
But my question is altogether different. Why is it the compiler allows an abstract method to be default but not private?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Because with default access there can still be a class in the same package that implements the method. With private, no other class can see the method, so they also cannot implement it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Moha Shaf:
Thanks for your reply.
But my question is altogether different. Why is it the compiler allows an abstract method to be default but not private?


An abstract method is like a contract stating "This method will be implemented in an extending class". However, private methods are not visible to an extending class. Private methods are only visible in the class in which they're declared. So a private method can never be declared abstract.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
Don't get me started about those stupid light bulbs.
 
subject: why private and abstract connot go together?
 
Similar Threads
Access Modifiers
Why a class declared as Abstract, cannot have any abstract method declaration?
basic java question...please advice
How does abstarct work for default methods declared in diff packages?
Can anyone explain why the following doesnt compile...