Introduction to Object-Oriented Programming

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

The class Object. Lecture CS1122 Summer 2008

Expected properties of equality

CSE 331 Software Design & Implementation

C12a: The Object Superclass and Selected Methods

CSE 331 Software Design & Implementation

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

Software Development (cs2500)

Introduction to Object-Oriented Programming

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

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

Equality. Michael Ernst. CSE 331 University of Washington

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

The Java Type System (continued)

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

CISC370: Inheritance

java.lang.object: Equality

Rules and syntax for inheritance. The boring stuff

Building Java Programs. Inheritance and Polymorphism

Equality. Michael Ernst. CSE 331 University of Washington

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

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Java Magistère BFA

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

PIC 20A Number, Autoboxing, and Unboxing

Introduction to Inheritance

Collections Algorithms

Java Fundamentals (II)

Introduction to Object-Oriented Programming

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

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Methods Common to all Classes

Object Oriented Programming: Based on slides from Skrien Chapter 2

CS 1331 Exam 3 Practice

MIT EECS Michael Ernst Saman Amarasinghe

Software Construction

Implementing Object Equivalence in Java Using the Template Method Design Pattern

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Java: introduction to object-oriented features

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS 1331 Exam 3 Practice ANSWER KEY

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Equality for Abstract Data Types

CSE 331 Spring 2018 Midterm

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

COMP 250 Fall inheritance Nov. 17, 2017

Introduction to Object-Oriented Programming

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

equals() in the class Object

Super-Classes and sub-classes

More On inheritance. What you can do in subclass regarding methods:

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

The Liskov Substitution Principle

CS11 Java. Winter Lecture 8

CLASS DESIGN. Objectives MODULE 4

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

CS-202 Introduction to Object Oriented Programming

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

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

Abstract Classes and Interfaces

CS 2340 Objects and Design - Scala

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Practice for Chapter 11

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Chapter 11: Collections and Maps

Why OO programming? want but aren t. Ø What are its components?

Operators and Expressions

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

Programming Languages and Techniques (CIS120)

INHERITANCE. Spring 2019

Computer Science 225 Advanced Programming Siena College Spring Topic Notes: Inheritance

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

UMBC CMSC 331 Final Exam

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

C a; C b; C e; int c;

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

CS260 Intro to Java & Android 03.Java Language Basics

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

CS Programming I: Inheritance

Classes and Inheritance Extending Classes, Chapter 5.2

Distributed Systems Recitation 1. Tamim Jabban

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CS 2340 Objects and Design

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

Objects and Iterators

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

Subtyping. Lecture 13 CS 565 3/27/06

Inheritance & Polymorphism

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Transcription:

Introduction to Object-Oriented Programming Object-Oriented Programming, Part 2 of 3 Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 1 / 16

Fitting Classes Into the Java Hierarchy java.lang.object defines several methods that are designed to be overriden in subclasses (JLS 4.3.2:) The method equals(object) defines a notion of object equality, which is based on value, not reference, comparison. The method hashcode is very used together with equals(object) in hashtables such as java.util.hashmap. The method tostring returns a String representation of the object. The method clone is used to make a duplicate of an object (don t touch). The method finalize is run just before an object is destroyed (don t touch). A class hierarchy is also sometimes called a framework. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 2 / 16

When to Override the equals(object) Method The default implementation of equals(object) in java.lang.object is object identity - each object equals(object) only itself. When should a class override equals(object)? When logical equality differs from object identity, as is the case for value classes like Date When classes don t implement instance control. Instance control means that a class ensures that there is only one instance of a class. When a suitable equals(object) method is not provided by a superclass. More important than recognizing when to override equals(object) is knowing how to override equals(object). CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 3 / 16

How to Override the equals(object) Method Obey the general contract of equals(object) (JLS ), which says that equals(object) defines an equivalence relation. So, for non-null references, equals(object) is reflexive - any object equals(object) itself symmetric - if a.equals(object)(b) is true then b.equals(a) must be true transitive - if a.equals(b) and b.equals(c) are true then a.equals(c) must be true consistent - if a and b do not change between invocations of a.equals(b) or b.equals(a) then each invocation must return the same result a.equals(null) must always return false. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 4 / 16

A Recipe for Implementing equals(object) Obeying the general contract of equals(object) is easier if you follow these steps. 1 Ensure the other object is not null. 2 Check for reference equality with == (are we comparing to self?). 3 Check that the other object is an instanceof this object s class. 4 Cast the other object to this s type (guaranteed to work after instanceof test) 5 Check that each significant field in the other object equals(object) the corresponding field in this object. After seeing an example applicaiton of this recipe we ll motivate the proper implementation of equals(object) methods by introducing our first collection class, ArrayList. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 5 / 16

An Example equals(object) Method Assume we have a Person class with a single name field. 1 Ensure the other object is not null. 2 Check for reference equality with == (are we comparing to self?). 3 Check that the other object is an instanceof this object s class. 4 Cast the other object to this s type (guaranteed to work after instanceof test) 5 Check that each significant field in the other object equals(object) the corresponding field in this object. Applying the recipe: public boolean equals(object other) { 1: if (null == other) { return false; 2: if (this == other) { return true; 3: if (!(other instanceof Person)) { return false; 4: Person that = (Person) other; 5: return this.name.equals(that.name); CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 6 / 16

Arrays and ArrayList Arrays are fixed-size collections of any data types, including primitives ArrayLists are dynamically-allocated (i.e., automatically resized) collections of reference types (not primitives - but we ll talk about autoboxing). ArrayLists use arrays internally, but this isn t important to know for basic use. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 7 / 16

ArrayList Basics Create an ArrayList with operator new: ArrayList tasks = new ArrayList(); Add items with add(): tasks.add("eat"); tasks.add("sleep"); tasks.add("code"); Traverse with for-each loop: for (Object task: tasks) { System.out.println(task); Note that the for-each loop implicitly uses an iterator. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 8 / 16

Primitives in Collections ArrayLists can only hold reference types. So you must use wrapper classes for primitives: ArrayList ints = new ArrayList(); ints.add(new Integer(42)); Java auto-boxes primitives when adding to a collection: ints.add(99); But auto-unboxing can t be done when retrieving from an untyped collection: int num = ints.get(0); // won t compile The old way to handle this with untyped collections is to cast it: int num = (Integer) ints.get(0); // auto-unboxing on assignment to int We ll see a better way to handle this with generics. See ArrayListPrimitivesDemo.java for more. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 9 / 16

The equals(object) Method and Collections A class whose instances will be stored in a collection must have a properly implemented equals(object) method. The contains method in collections uses the equals(object) method in the stored objects. The default implementation of equals(object) (object identity - true only for same object in memory) only rarely gives correct results. Note that hashcode() also has a defualt implementation that uses the object s memory address. As a rule, whenever you override equals(object), you should also override hashcode 1. 1 hashcode() is used in objects that are keys in Maps. You ll learn about Maps later in the course. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 10 / 16

equals(object) Method Examples In this simple class hierarchy, FoundPerson has a properly implemented equals(object) method and LostPerson does not. abstract static class Person { public String name; public Person(String name) { this.name = name; static class LostPerson extends Person { public LostPerson(String name) { super(name); static class FoundPerson extends Person { public FoundPerson(String name) { super(name); public boolean equals(object other) { if (this == other) { return true; if (!(other instanceof Person)) { return false; return ((Person) other).name.equals(this.name); Let s examine the code in ArrayListEqualsDemo.java. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 11 / 16

Override-Equivalence Two methods are override-equivalent if: they have the same name, they have the same parameter lists, and their return values are covariant These rules lead to a few pitfalls: You can t define override-equivalent methods in the same class. In subclasses it s easy to accidentally overload a superclass method when you meant to override. Let s look at a few examples to help us understand these rules. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 12 / 16

Covariant Returns covariantreturn is covariant in Person and LostPerson: abstract static class Person { public String name; public Person(String name) { this.name = name; public Object covariantreturn() { return new Object(); static class LostPerson extends Person { public LostPerson(String name) { super(name); @Override public LostPerson covariantreturn() { return this;... because LostPerson is a subtype of Object. But SubLostPerson won t compile: static class SubLostPerson extends LostPerson { public SubLostPerson(String name) { super(name); @Override public Person covariantreturn() { return this;... becuase its covariantreturn s return type is contravariant, that is, Person is a supertype of LostPerson. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 13 / 16

Non-Covariant Return NoncompilingPerson won t compile because int is not a subtype of boolean and because return-type covariance only applies to reference types, not primitives. static class NonCompilingPerson extends Person { public NonCompilingPerson(String name) { super(name); /** * This method won t compile because int is not a subtype of boolean. */ public int equals(object other) { if (null == other) return 0; if (this == other) return 1; if (!(other instanceof Person)) return 0; return ((Person) other).name.equals(this.name)? 1 : 0; CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 14 / 16

Accidental Overloading It s easy to make this mistake: static class OverloadedPerson extends Person { public OverloadedPerson(String name) { super(name); public boolean equals(overloadedperson other) { if (null == other) { return false; if (this == other) { return true; if (!(other instanceof OverloadedPerson)) { return false; return ((OverloadedPerson) other).name.equals(this.name); Signature of equals in Object is public boolean equals(object other) - parameter type is Object. In OverloadedPerson we ve accidentally overloaded equals instead of overriding equals by making the parameter type OverloadedPerson. Using the @Override annotation helps you avoid this mistake. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 15 / 16

Closing Thoughts java.lang.object is the root superclass of every Java class. "Classic" Java collections are general because they hold elements of type Object Java collections and many programming idioms rely on the methods defined in Object, some of which must be overridden in subclasses in order for instances of these subclasses to function properly as elements of collections. Overriding equals is straightforward if you follow the recipe. There s more to overriding equals in particular, overriding hashcode but now we know the basic concepts. CS 1331 (Georgia Tech) Object-Oriented Programming, Part 2 of 3 16 / 16