CS 110: Introduction to Computer Science Spring 2008

CS 110: Introduction to Computer Science Spring 2008

<p>CS 110: Introduction to Computer Science Spring 2008</p><p>Information Hiding Or ‘what is that public keyword that teacher’s been waving his hand at?’</p><p>Purpose: Restrict client programmer from access to some code/data </p><p>Is this a way for a programmer/company to safeguard code they wrote? NO</p><p>Information hiding is about restricting use so that the client can safely and correctly make use of a class.</p><p>Two objects communicating is like two people communicating—you don’t want total access!</p><p>Rules of public, private, protected:</p><p> public: any code can access the data/method private: only methods of the same class can access data/method protected: only subclasses and classes in same package can access data/method</p><p>Instructor sample: write some code to show rules</p><p>Hiding data Most coding teams use the convention that all data members declared as private.</p><p> class Foo { public int x; // most programming conventions say this is a no-no private int y;</p><p>}</p><p>Why? Forces client programmer to call a method to do anything. That method can check parameters for validity.</p><p>Consider Java’s ArrayList class or the ArrList class you wrote.</p><p>Should the built-in java array defined within it be public/private?</p><p>How about the count kept for how many elements?</p><p>How could the programmer screw up if these were public?</p><p>If you do want to allow access to a data member, need a public method to do so, sometimes as simple as a ‘set’ and ‘get’ function CS 110: Introduction to Computer Science Spring 2008</p><p>Sample set and get methods:</p><p> public void setX(int x) { // check validity of parameter x, e.g., if (x>0) this.x=x;</p><p> public int getX() { return x; }</p><p>Note that Eclipse will generate these for you!</p><p>Private Methods Methods that are 'sub-functions' of others should be made private as they should only be called by methods within the class, not by other classes or main.</p><p>What methods in Lexer could be private?</p><p>In-class assignment</p><p>1. Change a data member in your current program to private, then write code that incorrectly attempts to access this data member. Show your instructor when you get such an error.</p><p>2. Change a method in your current program to private, then write code that incorrectly attempts to access this method. Show your instructor when you get such an error.</p><p>3. Modify your Lexer so that it correctly marks what should and should not be private.</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us