• Post Reply Bookmark Topic Watch Topic
  • New Topic

private final class???  RSS feed

 
Amir Jan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, please explain why someone would declare such an inner class?
and especially why final if its a private class?

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, he probably declared it private so that it could not be accessed outside of its enclosing class. That's what private is for.

And he probably declared it as final so it couldn't be extended. That's what final is for.

Of course, given that it's already private, it could only be overridden by somebody modifying the enclosing class anyway, and if we're qualified to be in there doing that, then we're probably also qualified to decide it we want to extend that class. So final doesn't really add any behavioral benefit in any practical sense.

Maybe the author was just being pedantic, and wanted to clearly express his intents for that class. I could see doing that myself.
 
Amir Jan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the very fast reply.
I am thinking that you are correct but the problem is that the author is the Android development team.
this is the code which i saw: (from this link http://developer.android.com/guide/components/services.html )


so can you see any other reasons to that(other then what you just wrote)?

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a general philosophy that you should restrict things as much as you can, and open them up only where necessary. This is because it's easy to change things to be less restrictive later, but it's not so simple to make things more restrictive because you might break other code.

So, based on that, the inner class is made private because there is no need for it to be accessed outside the containing class. And it's made final because there's no reason to inherit from it.

If a class isn't designed to be inherited from, it's probably good practice to make it final. I usually wouldn't bother doing it, but it's still a good idea.
 
Amir Jan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i understood.

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amir Jan wrote:Thanks for the very fast reply.
I am thinking that you are correct but the problem is that the author is the Android development team.


That makes no difference.

They're human, subject to quirks and mistakes like anybody else.

Like I said, I could see myself doing that, and I don't consider them necessarily any better at OO design or Java implementation than I am.

[EDIT: And no, I didn't mean that as arrogantly as it may sound. I just mean, if I can do it, so can they.]
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another “general philosphy” is that you need not know about implementation details of the software you are using. The class was probably private so you would not know about it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!