help,java help urgently needed. pleasee help


    Rep:
    help got a problem
    i got an exam tomorrow and was just wandering
    in a program if u have two attributes with different access modifiers- one private and one public, how would you define one of the attributes to break the encapsulation principle? sorry its a question i know is gonna come up
    my friend said:
    answer: make the attribute which is private, public. this is to modify the value of that attribute.
    please say he is right someone
    whats the answer??

    Rep:
    Yes, I'd say that is correct.

    The encapsulation principle is really exactly what it sounds - that each class should be encapsulated. This means that all attributes that don't absolutely need to be public shouldn't be, and that all classes should essentially manage and have exclusive access to their own attributes. Having a class attribute as public allows other external classes to modify its value, which breaks this principle. This might not be a problem in some cases and is sometimes used if the programmer doesn't want to have access methods (getX() and setX()), but can cause problems in some circumstances (e.g. where the value is restricted somehow - it could be set to an invalid value by another class).

    Hope this helps a little. Good luck with the exam

    Rep:
    (Original post by bones)
    Yes, I'd say that is correct.

    The encapsulation principle is really exactly what it sounds - that each class should be encapsulated. This means that all attributes that don't absolutely need to be public shouldn't be, and that all classes should essentially manage and have exclusive access to their own attributes. Having a class attribute as public allows other external classes to modify its value, which breaks this principle. This might not be a problem in some cases and is sometimes used if the programmer doesn't want to have access methods (getX() and setX()), but can cause problems in some circumstances (e.g. where the value is restricted somehow - it could be set to an invalid value by another class).

    Hope this helps a little. Good luck with the exam
    Thanks so much.. i know now to say make the attribute which is public, private.

Reply

Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?

    this is what you'll be called on TSR

  2. this can't be left blank
    this email is already registered. Forgotten your password?

    never shared and never spammed

  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. By joining you agree to our Ts and Cs, privacy policy and site rules

  2. Slide the button to the right to create your account

    Slide to join now Processing…

: December 15, 2005

Which unis are the poshest?

See what students perceive to be the classiest establishments in Britain

Reputation gems:
You get these gems as you gain rep from other members for making good contributions and giving helpful advice.