Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Question 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?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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...:)

  4. #4
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by buckskinner1776 View Post
    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.
    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

  1. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 11:51 PM
  2. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 03:14 PM
  3. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 07:46 AM
  4. java-Encapsulation
    By lenah in forum Advanced Java
    Replies: 5
    Last Post: 01-05-2009, 10:05 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •