CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
Effective Java, Chapter 3: Methods Common to All Objects.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
More about inheritance Exploring polymorphism 5.0.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Object-oriented Programming in Java. What is OOP?  The goal is (subtype) polymorphism  Achieved by Classes (user-defined types) Classes (user-defined.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
CS1101: Programming Methodology Aaron Tan.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Topic 4 Inheritance.
Overriding toString()
More about inheritance Exploring polymorphism 5.0.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Java Type System and Object Model Horstmann ch , 7.7.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
Object-oriented Programming in Java
CSC 205 Java Programming II
More about inheritance
Lecture 17: Polymorphism (Part II)
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
COMP 110 Some notes on inheritance, review
Variables as Remote Control
Recitation 6 Inheritance.
Overloading and Constructors
Polymorphism.
Extending Classes.
CSC 113: Computer programming II
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Generic Classes and Methods
Effective Java, Chapter 3, 3rd Edition: Methods Common to All Objects
Overriding methods from the Object class: equals, toString
Lecture 18: Polymorphism (Part II)
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Making a copy of an object: shallow and deep copy
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CIS 110: Introduction to computer programming
Presentation transcript:

CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString

CSC142 NN 2 Object class  The ultimate superclass  Any object is an Object MyClass c = new MyClass(); System.out.print(c instanceof Object); //prints true  All methods from Object are inherited by any other class  Should they be overridden?  Take a look at toString and equals.

CSC142 NN 3 public String toString() (1) public class Car { private String make; private double weight; public Car(String theMake, double theWeight) { make = theMake; weight = theWeight; } }  Car c = new Car(“Ford”, 2000); System.out.println(c.toString()); // prints // name of the class + some hash code  Override toString in Car to make it more meaningful

CSC142 NN 4 public String toString() (2) public class Car { private String make; private double weight; public Car(String theMake, double theWeight) { make = theMake; weight = theWeight; } public String toString() { return “make = “ + make + “, weight = “ + weight; } }  Car c = new Car(“Ford”, 2000); System.out.println(c.toString()); // prints make = Ford, weight =  Always override toString()

CSC142 NN 5 public boolean equals(Object o)  Implemented in Object as public boolean equals(Object o) { return this == o; }  OK? Car c1 = new Car(“Ford”, 2000); Car c2 = new Car(“Ford”, 2000); System.out.println(c1.equals(c2)); // prints false  Fix: override equals within Car

CSC142 NN 6 equals (2) public class Car { // previous code omitted public boolean equals(Object o) { if (o instanceof Car) { Car c = (Car) o; return (this.weight == c.weight && this.make.equals(c.make)); } else { return false; } } }  o must be a Car: check with instanceof  Use equals to compare fields that have a reference type (such as String).

CSC142 NN 7 Does it work?  Car c1 = new Car(“Ford”, 2000); Car c2 = new Car(“Ford”, 2000); System.out.println(c1.equals(c2)); // prints true.  But wait!  What if Car is inherited?

CSC142 NN 8 equals and inheritance public class FancyCar extends Car{ private double topSpeed; public FancyCar(String theMake, double theWeight, double theTopSpeed){ super(theMake, theWeight); topSpeed = theTopSpeed; } public boolean equals(Object o) { if (o instanceof FancyCar) { FancyCar fc = (FancyCar) o; return (super.equals(o) && this.topSpeed == o.topSpeed); } else { return false; } } }  Car c = new Car(“Ford”, 2000); FancyCar fc = new FancyCar(“Ford”, 2000, 200); System.out.print(c.equals(fc)); // prints true System.out.print(fc.equals(c)); // prints false

CSC142 NN 9 What is going on?  A FancyCar is a Car fc instanceof Car is true  A Car is not a FancyCar c instanceof FancyCar is false  One requirement of equals is that if x.equals(y) is true, then y.equals(x) is also true.  instanceof checks an is_a relationship  A necessary condition for two variables to be equal is that they have the same dynamic type.  Get the dynamic type with getClass(). Don’t use instanceof.

CSC142 NN 10 A better equal  In Car public boolean equals(Object o) { if (o != null && o.getClass() == this.getClass()) { Car c = (Car) o; return (this.weight == c.weight && this.make.equals(c.make)); } else {return false;} }  In FancyCar public boolean equals(Object o) { if (o != null && o.getClass() == this.getClass()) { FancyCar fc = (FancyCar) o; return (super.equals(o) && topSpeed==fc.topSpeed); } else {return false;} }

CSC142 NN 11 One last word  Check the class website for the complete code of the previous examples  If equals is overridden, override hashcode as well. See later in 143…