Results are out! Find what you need...fast. Get quick advice or join the chat
Hey there! Sign in to join this conversationNew here? Join for free to post

help,java help urgently needed. pleasee help

Announcements Posted on
Applying to Uni? Let Universities come to you. Click here to get your perfect place 20-10-2014
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    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??
    • 0 followers
    Offline

    ReputationRep:
    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
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (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?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  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 to join now Processing…

Updated: December 15, 2005
New on TSR

Submitting your UCAS application

How long did it take for yours to be processed?

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