Hey there Sign in to join this conversationNew here? Join for free

help,java help urgently needed. pleasee help

Announcements Posted on
Post on TSR and win a prize! Find out more... 10-04-2014
Interview Discussion 30-01-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?

    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 completing the slider below you agree to The Student Room's terms & conditions and site rules

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

    Slide to join now Processing…

    You don't slide that way? No problem.

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