Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS1054: Lecture 11 More Sophisticated Behavior (contd..)"— 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: book1 == book2 ? “Lord of the Rings” :Book book1book2 “Harry Porter” :Book

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

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

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

8 Identity vs equality (Strings) "bye" :String input "bye" :String String input = reader.getInput(); if(input.equals("bye")) {... } equals tests equality equals ?  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.


Download ppt "CS1054: Lecture 11 More Sophisticated Behavior (contd..)"

Similar presentations


Ads by Google