Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why private and abstract connot go together?

 
Moha Shaf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic