The class Object. Lecture CS1122 Summer 2008

Similar documents
equals() in the class Object

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Super-Classes and sub-classes

Methods Common to all Classes

java.lang.object: Equality

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Programming Languages and Techniques (CIS120)

For this section, we will implement a class with only non-static features, that represents a rectangle

Expected properties of equality

CSE 331 Software Design & Implementation

Chapter 11: Collections and Maps

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

C12a: The Object Superclass and Selected Methods

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Object-Oriented Programming in the Java language

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Programming Languages and Techniques (CIS120)

CSE 331 Software Design & Implementation

Programming Languages and Techniques (CIS120)

COMP 250 Fall inheritance Nov. 17, 2017

Polymorphism. return a.doublevalue() + b.doublevalue();

Equality. Michael Ernst. CSE 331 University of Washington

Programming Languages and Techniques (CIS120e)

Domain-Driven Design Activity

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

The Liskov Substitution Principle

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Java Fundamentals (II)

Uguaglianza e Identità. (no, non avete sbagliato corso )

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Programming Languages and Techniques (CIS120)

Abstract Classes and Interfaces

Introduction to Object-Oriented Programming

Java Magistère BFA

type conversion polymorphism (intro only) Class class

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Java Object Model. Or, way down the rabbit hole

INHERITANCE. Spring 2019

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Software Engineering Design & Construction

Programming Languages and Techniques (CIS120)

Inheritance. Transitivity

Java Classes, Inheritance, and Interfaces

CLASS DESIGN. Objectives MODULE 4

Equality. Michael Ernst. CSE 331 University of Washington

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Inheritance (Part 5) Odds and ends

MIT AITI Lecture 18 Collections - Part 1

Programming Languages and Techniques (CIS120)

MET08-J. Preserve the equality contract when overriding the equals() method

Building Java Programs. Inheritance and Polymorphism

Rules and syntax for inheritance. The boring stuff

Announcements/Follow-ups

cs2010: algorithms and data structures

More about inheritance

ITI Introduction to Computing II

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

ITI Introduction to Computing II

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #4. Review: Methods / Code

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

You have seen abstractions in many places, lets consider them from the ground up.


The Java Type System (continued)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS/ENGRD 2110 SPRING 2018

Advanced Topics on Classes and Objects

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Equality for Abstract Data Types

Java Persistence API (JPA) Entities

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

Elementary Symbol Tables

Software Development (cs2500)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

ELEMENTARY SEARCH ALGORITHMS

Effective Java. Object Equality. - Case Studies - Angelika Langer. Trainer/Consultant.

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Charlie Garrod Michael Hilton

Everything is an object. Almost, but all objects are of type Object!

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

COM1020/COM6101: Further Java Programming

11 HashMap: Overriding equals ; JUnit; Vistors

Java: advanced object-oriented features

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Introduction to Inheritance


CS 211: Inheritance. Chris Kauffman. Week 5-2

Not overriding equals

PIC 20A Number, Autoboxing, and Unboxing

Transcription:

The class Object http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Lecture 10 -- CS1122 Summer 2008

Review Object is at the top of every hierarchy. Every class in Java has an IS-A relationship with Object. Methods of the class Object are available from any reference. Polymorphism allows you to override the default functionality of methods inherited from the class Object.

Object If you go to the Java API, you will see that the class Object has 11 methods. You can override 5 of those methods: equals, hashcode, tostring, clone, finalize We ll talk about the first three, today. To learn about the others, visit: http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Next week we ll talk about why you can t override some methods.

overriding Object s methods The methods in the class Object are well defined in the API. You must be sure adhere to everything documented in the API. Also think about how your implementation will affect subclasses. Remember, subclasses would use your method before using the default in Object.

overriding Object s methods parameters will be references to Objects this allows a reference to anything to be passed into these methods to make sure you have the right kind of reference, use the getclass() method

getclass() public class Abc{ } private int x = 5; public void doit(object o){ if(o.getclass().equals(this.getclass())){ System.out.println( o references an Abc Obj); }else{ System.out.println( Not an Abc object ); } }

overriding Object s methods parameters will be references to Objects once you know you have a reference to the kind of object you are expecting, you need to use a cast to access parts of that object

casting references public class Abc{ } private int x = 5; public void doit(object o){ if(o.getclass().equals(this.getclass())){ (Abc)o.x = 13; }else{ System.out.println( Not an Abc object ); } }

public boolean equals(object o) Used to test the equality of objects Defaults to the same as == operator tests to see if 2 references point at the same object Override to compare the contents of objects Example: String s = hello ; String t = hello ; if(s.equals(t)){ System.out.println( world ); }

public boolean equals(object o) According to the API, your implementation must be: reflexive: for any non-null reference value x, x.equals(x) should return true. symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

public boolean equals(object o) According to the API, your implementation must be: consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false.

a very brief intro to hashing hashing a fast way of storing/retrieving objects in a Vector, you store objects at a given index in a Hashtable (HashMap, HashSet, etc.), you store objects in a location based on a hash code a hash code is calculated based on the object being stored, instead of being based on the structure it is stored in. hashing is covered in Data Structures class

public int hashcode() this method relies on equals() you should override these methods together According to the API, your implementation must: return the same number for any 2 objects that are equal according to equals() Be careful when using values that can change to calculate the hashcode

public String tostring() by default it prints classname@hashcode this is not very useful in most cases override to give useful information it is recommended that you override this method in every class that you write very helpful for debugging!

Summary the methods in Object exist they are very useful when defined properly the definitions of the methods in Object are very general override methods in Object to provide implementations that are specific to your classes always be sure to follow the definitions given in the API