Just one sec...
Hey there! Sign in to join this conversationNew here? Join for free to post

help,java help urgently needed. pleasee help

Announcements Posted on
Results day: make sure you know what to expect before you get your grades. Here's how to be ready. 22-07-2016
    • Thread Starter
    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??
    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
    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. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: December 15, 2005
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Best social network/media?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22

Registered Office: International House, Queens Road, Brighton, BN1 3XE

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