Results 1 to 5 of 5
Thread: encapsulation & constructors
- 05-03-2010, 10:50 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
encapsulation & constructors
I have a little experience but just getting started with formal training. I couldn't find information on my question elsewhere in this forum so here it is. Hopefully it's clear.
We've been learning encapsulation and constructors this week. I had an assignment that required creating a default constructor for a Rectangle class and to overload this constructor with another that would accept a height and width. That was no problem.
It was also required that we provided get and set methods for both height and width. Again, this was no problem.
My question comes from the requirement that the custom constructor would validate the height and width provided such that they were both > 1 and < 30. Again in and of itself, no problem. But this got me wondering about the set methods. How would I do the same validation for the set method?
The assignment didn't ask for it, but if it was a real world thing, my thought is that the height and width would have to be > 1 and < 30 at all times. Not just on instantiating a new Rectangle. By the assignment, I could meet the criteria for instantiation but then later use the set method to increase the dimensions out of the limits.
So my next thought was why not do the validation within the set methods rather than in the constructor. The constructor could then invoke the set method and pass the appropriate arguments that were provided. In this manner, my validation code would only be in the set method and not both the set method and constructor.
Is there a problem with this direction?
- 05-03-2010, 10:58 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Excellent direction, do exactly that. Always avoid duplication of code - if you write the same thing twice, a year later when it's time to change things you'll change it in one place and forget about the other.
- 05-04-2010, 11:13 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
First off you spotted a possible requirement hole. And then a solution to it, which involves reducing the amount of code to maintain...and also asked to double check that your thinking is correct.
You're not a regular posting under a pseudonym are you? ;)
Posts like this make Tolls smile...:)
- 05-10-2010, 06:25 PM #4
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
I've done a little bit of programming over the past twenty years. Enough to be dangerous. I can usually get it to work, sometimes cleaner than others.
Java is a new one to me that I'm taking a course in. If I understood the instructor correctly, I shouldn't do it in this manner. Something to do with the constructor being early in the life of an object. I've always been against duplicating code and it just didn't make sense to me.
I've been around enough to know I am only dangerous and that all sorts of people have all sorts of experience, so I was looking for more input on why I shouldn't do it like this.
- 05-10-2010, 06:35 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,415
- Blog Entries
- 7
- Rep Power
- 17
If you encounter a width or height parameter that is/are out of range, there are two things you can do: 1) correct the value or 2) throw an IllegalArgumentException. If you do the latter and have your constructor call the setWidth( ... ) and setHeight( ... ) method and one of them throws that exception no rectangle will be constructed at all. That is the way I prefer it: I don't want objects with invalid state at all.
kind regards,
Jos
Similar Threads
-
Constructors?
By annna in forum New To JavaReplies: 3Last Post: 01-27-2010, 10:51 PM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM -
java-Encapsulation
By lenah in forum Advanced JavaReplies: 5Last Post: 01-05-2009, 09:05 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks