• Post Reply Bookmark Topic Watch Topic
  • New Topic

Eencapsulated class  RSS feed

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How can one create a well-encapsulated class?What are the principles to be followed?

If there are a lot of ways to do so, please list some?

Thanks in advance.



 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:How can one create a well-encapsulated class?What are the principles to be followed?
If there are a lot of ways to do so, please list some?

OK. In no particular order, and not necessarily exclusive:

1. Make its fields private. ALWAYS.
2. Don't provide "setter" methods unless they're absolutely necessary.
3. Don't provide "getter" methods unless they add something useful to the API.
4. Provide ONLY those public methods required to use it (follows on from #3).
5. Consider making it immutable (look it up).
6. Have it implement an interface - or two, or three...
7. TELL; don't ask.

Hope it helps - and I've probably left out several things - but I'm not sure that a mere list will help you much.

If you have any more specific questions, feel free to come back.

Winston
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

Hope it helps - and I've probably left out several things - but I'm not sure that a mere list will help you much.

Winston



Yeah it sure helps and i will try to put it in practice.

The last tip "tell, don't ask" is quite interesting also.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

If you have any more specific questions, feel free to come back.

Winston


Yeah i got another one: What does a bad-encapsulated class contain or look like? This might help me avoid common mistake.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:Yeah i got another one: What does a bad-encapsulated class contain or look like? This might help me avoid common mistake.

Well, pretty much anything that violates the list above, but the most common ones I can think of:
1. public fields.
2. "Setters" for everything.
3. public methods that don't add anything to the API (including unneeded "getters").

I know I'll get a lot of flak for this, but one example of that last point is:
List.contains(whatever)
and
List.indexOf(whatever)

If you have that last method, you don't need the first one; which makes it redundant (because indexOf() returns -1 if 'whatever' was not found). Now, you could certainly argue for the inclusion of contains() on the grounds of "readability", but the fact is that in logical terms it IS redundant.

The fact is that there are no hard and fast "rules" about stuff like this. You need to do what you think best, and what "makes sense" for your class - and never forget that it is yours - but before you write any method, it's worth considering whether it's really needed or not.

HIH

Winston
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would never give you flak, but I would disagree with you. I seem to do that all the time.
A contains() method is a convenience to make it unnecessary to write indexOf(foo) < 0. It is also more object‑oriented that the List class does the work of finding whether it contains foo or not.
Remember the index of -1 is a leftover from C.

Also, I think the designers of library classes have the idea that you should provide as many methods as possible just in case somebody ever needs them. Or things in case nobody ever needs them, like some of the String constructors
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree that they are using redundancy of methods to gain readability. They were also trying to recover from an API where the only collections were Hashtable Vector and Stack.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Also, I think the designers of library classes have the idea that you should provide as many methods as possible just in case somebody ever needs them.

Which is the absolute antithesis of what I was taught (having been a "library" bod - or "toolmaker" - for many years). IMO, it's even more important for a "library" class to provide ONLY those methods needed to "use" it.

To my mind, the List API is a classic example of a "library" API gone mad. It defines 28 methods on top of the 3 it inherits directly from the Collection interface it extends; about half of which are concerned solely with dealing with collections of objects (addAll() being a prime example), and several others of which are optional. No wonder they need AbstractList to back it up.

To my mind, it's doubly important to be "minimalist" when you're designing library (or foundation) classes or interfaces, because some poor sod is going to have to actually use them or (even worse) implement them.

As regards contains(), I have no particularly strong views; I just cited it as an example. The fact is that contains() and indexOf() perform different functions, and I can certainly imagine that there might be a structure for which the first is significantly faster than the second. I've just yet to meet it.

A slight digression from the brief I suspect; but until raghav replies...

Winston
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

but until raghav replies...

Winston


Maybe in a year or two i will reply....for now you are the man...
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought contains would be faster than index when it runs in constant time (hash collections, but they don't have an index) or when it runs in logarithmic time (trees, where you can probably find indices depending on the traversal you use). An index method would run in linear time.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Campbell Ritchie wrote:Also, I think the designers of library classes have the idea that you should provide as many methods as possible just in case somebody ever needs them.

Which is the absolute antithesis of what I was taught (having been a "library" bod - or "toolmaker" - for many years). IMO, it's even more important for a "library" class to provide ONLY those methods needed to "use" it.

I was just talking about this with my colleagues during lunch: the YAGNI principle.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I was just talking about this with my colleagues during lunch: the YAGNI principle.

Ah yes, a fine old chestnut; and as true today as it was when it came out - and incredibly difficult to get right.

There's no doubt that Java spent a lot of time considering the "fundamentals" of their Collections Framework, because there are some really good things in the docs - which I often use when I'm trying to explain the distinctions between various types of "collection".

I fear, however, that the implementation was left to people who weren't as enlightened...

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:Maybe in a year or two i will reply....for now you are the man...

Well, thanks for those kind words. I'm not sure everyone here would agree...

However, getting back on point: Have we answered your question?

Winston
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

Have we answered your question?

Winston


Enough to get me started.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I would have thought contains would be faster than index when it runs in constant time (hash collections, but they don't have an index) or when it runs in logarithmic time (trees, where you can probably find indices depending on the traversal you use). An index method would run in linear time.

Would it?

First off: Maps don't generally have indexes, so I suspect that the point is moot.

Second: indexOf() is peculiar to a List, which is an extension of a Collection; so, on that basis, I guess you could say that is it IS reasonable. I still say that it's logically redundant - and I'd be really interested to know if anyone knows of a List where contains() is not implemented as:

return indexOf(whatever) < 0;

The only one I can think of is a SkipList, which doesn't really tick all the boxes for a Java List.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:Enough to get me started.

Great!

You'll have to excuse the musings. It's Friday.

Winston
 
Greenhorn
Posts: 12
Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First decide what your class is going to do. This stuff is public.

Then decide how your class is going to do it. This stuff is private.

And that's encapsulation.

One way to go through this process is to declare your public methods, but to make them empty. Just throw "UnsupportedOperationException" in the methods. Then fill the implementation in.



When the TODO!s are all gone, you have finished stage one of writing a class that does something.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!