Overriding toString()

Slides:



Advertisements
Similar presentations
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Searching and Sorting I 1 Searching and Sorting 1.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Determine whether each curve below is the graph of a function of x. Select all answers that are graphs of functions of x:
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Understanding Inheritance CompSci 230 S Software Design and Construction.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Java Implementation: Part 3S CompSci 230 Software Construction.
CompSci Data Types & Operations. CompSci The Plan  Overview  Data Types  Operations  Code Samples  ChangeMaker.java  PolarCoordinate.java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
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[]
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
L ECTURE 6 Announcements. Next week will be great! Three major things are happening next week: – Zynga guest lecture – Your game pitches (more details.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Library Books exercise cosc The classes Library class +ArrayList -ArrayList + Library() +initializeDefaultCategories():void +displayAllCategories():void.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Identities, Contradictions and Conditional Equations.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
CompSci 230 S Programming Techniques
Searching.
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
CSC 205 Java Programming II
Lecture 17: Polymorphism (Part II)
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Implementing Polymorphism
More on Classes & Arrays
Класс Object Макаревич Л. Г..
Testing and debugging A short interlude 2-Dec-18.
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Building Java Programs
Recitation 8 October 14, 2011.
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Overriding methods from the Object class: equals, toString
Lecture 18: Polymorphism (Part II)
If-statements & Indefinite Loops
Testing and debugging A short interlude 17-Apr-19.
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
Presentation transcript:

Overriding toString() “You should always consider overriding the toString() method in your classes. “The Object’s toString() method returns a String representation of the object, which is very useful for debugging. The String representation for an object depends entirely on the object, which is why you need to override toString() in your classes.” http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

Overriding equals() The equals() method compares two objects for equality and returns true if they are equal. The equals() method provided in the Object class uses the identity operator (==) to determine whether two objects are equal. For primitive data types, this gives the correct result. For objects, however, it does not. The equals() method provided by Object tests whether the object references are equal—that is, if the objects compared are the exact same object. To test whether two objects are equal in the sense of equivalency (containing the same information), you must override the equals() method.

Example: testing an overridden equals() public static void main(String[] args){ Book firstBook = new Book("0201914670"); Book secondBook = new Book("0201914670"); if (firstBook.equals(secondBook)) { System.out.println("equivalent objects"); } else { System.out.println("non-equivalent objects"); } if (firstBook == secondBook){ System.out.println("two references to the same object"); System.out.println("references to different objects");

Exercise: overriding equals() public class Book { String Title; String Author; String Publisher; String Year; String ISBN; ... public boolean equals(Object obj) { // complete this: check ISBN for equality }

Answer public class Book { String Title; String Author; String Publisher; String Year; String ISBN; ... public boolean equals(Object obj) { if (obj instanceof Book) return ISBN.equals(((Book) obj).getISBN()); else return false; }