1 / 12

CS1054: Lecture 11

CS1054: Lecture 11. More Sophisticated Behavior (contd..). Today’s topics. Identity vs. equality Class variables or static variables Private vs. public accessors. Side note: String equality. String input = “bye”; if(input == "bye") { tests identity ... }

deepak
Download Presentation

CS1054: Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1054: Lecture 11 More Sophisticated Behavior (contd..)

  2. Today’s topics • Identity vs. equality • Class variables or static variables • Private vs. public accessors

  3. Side note: String equality String input = “bye”; if(input == "bye") { tests identity ... } if(input.equals("bye")) { tests equality ... } • Strings should (almost) always be compared with.equals

  4. Identity vs equality 1 Other (non-String) objects: :Book :Book “Lord of the Rings” “Harry Porter” book1 book2 book1 == book2 ?

  5. Identity vs equality 2 Other (non-String) objects: :Book :Book “Harry Porter” “Harry Porter” book1 book2 book1 == book2 ?

  6. Identity vs equality 3 Other (non-String) objects: :Book :Book “Harry Porter” “Harry Porter” book1 book2 book1 == book2 ?

  7. Identity vs equality (Strings) String input = reader.getInput(); if(input == "bye") { ... } == tests identity :String :String == ? "bye" "bye" input  (may be) false!

  8. Identity vs equality (Strings) equals tests equality String input = reader.getInput(); if(input.equals("bye")) { ... } :String :String ? equals "bye" "bye" input  true!

  9. Class variables.. aka.. static variables • ‘static’ keyword is used to define class variables • class variables are common to the entire class • All the objects share this variable • A static variable can be accessed from any of the class instances • Besides static variables … there are static methods (later in the semester).

  10. Constants private final int SIZE = 10; • private: access modifier, as usual • static: class variable • final: constant

  11. Public vs private • Public attributes (fields, constructors, methods) are accessible to other classes. • Fields should not be public. • Private attributes are accessible only within the same class. • Only methods that are intended for other classes should be public.

  12. Information hiding • Data belonging to one object is hidden from other objects. • Know what an object can do, not how it does it. • Information hiding increases the level of independence. • Independence of modules is important for large systems and maintenance.

More Related