Method2 method3 italic inherited behavior bold

Info icon This preview shows pages 14–25. Sign up to view the full content.

View Full Document Right Arrow Icon
method2 method3 Italic - inherited behavior Bold - dynamic method call Rain 1 Fog 1 Snow 2 Rain 2 Sleet 2 Snow 2 method3() Sleet 2 Snow 2 method3() Snow 3 Snow 3 Sleet 3 Fog 3
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
15 Example 1 • Example: Snow var1 = new Sleet (); var1.method2(); • Output: Sleet 2 Snow 2 Sleet 3 object variable
Image of page 15
16 Example 2 • Example: Snow var2 = new Rain (); var2.method1(); • Output: None! There is an error, because Snow does not have a method1 . variable object
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
17 Example 3 • Example: Snow var3 = new Rain (); (( Sleet ) var3).method2(); • Output: None! There is an error because a Rain is not a Sleet . object variable
Image of page 17
18 The Object class read 9.3
Image of page 18

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
19 Class Object • All types of objects have a superclass named Object . Every class implicitly extends Object • The Object class defines several methods: – public String toString() Returns a text representation of the object, often so that it can be printed. – public boolean equals(Object other) Compare the object to any other for equality. Returns true if the objects have equal state.
Image of page 19
20 Object variables • You can store any object in a variable of type Object . Object o1 = new Point(5, -3); Object o2 = "hello there"; Object o3 = new Scanner(System.in); • An Object variable only knows how to do general things. String s = o1.toString(); // ok int len = o2.length(); // error String line = o3.nextLine(); // error • You can write methods that accept an Object parameter. public void checkForNull( Object o ) { if (o == null) { throw new IllegalArgumentException(); } }
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
21 Recall: comparing objects • The == operator does not work well with objects. == compares references to objects, not their state. It only produces true when you compare an object to itself. Point p1 = new Point(5, 3); Point p2 = new Point(5, 3); if ( p1 == p2 ) { // false System.out.println("equal"); } ... x 5 y 3 p1 p2 ... x 5 y 3
Image of page 21
22 The equals method • The equals method compares the state of objects. if ( str1.equals(str2) ) { System.out.println("the strings are equal"); } • But if you write a class, its equals method behaves like == if ( p1.equals(p2) ) { // false :-( System.out.println("equal"); } This is the behavior we inherit from class Object . Java doesn't understand how to compare Point s by default.
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
23 Flawed equals method • We can change this behavior by writing an equals method. Ours will override the default behavior from class Object . The method should compare the state of the two objects and return true if they have the same x/y position. • A flawed implementation: public boolean equals(Point other) { if (x == other.x && y == other.y) { return true; } else { return false; } }
Image of page 23
24 Flaws in our method • The body can be shortened to the following: // boolean zen return x == other.x && y == other.y; • It should be legal to compare a Point to any object (not just other Point s): // this should be allowed Point p = new Point(7, 2); if ( p.equals("hello") ) { // false ...
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 25
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern