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

Similar documents
COMP 250 Fall inheritance Nov. 17, 2017

type conversion polymorphism (intro only) Class class

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

The class Object. Lecture CS1122 Summer 2008

COMP 250. inheritance (cont.) interfaces abstract classes

Methods Common to all Classes

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

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

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

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

Inheritance. Transitivity

equals() in the class Object

Super-Classes and sub-classes

java.lang.object: Equality

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

Expected properties of equality

CSE 331 Software Design & Implementation

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

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

Java Fundamentals (II)

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

Inheritance (Part 5) Odds and ends

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Introduction to Inheritance

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

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

Programming Languages and Techniques (CIS120)

CLASS DESIGN. Objectives MODULE 4

CSE 331 Software Design & Implementation

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

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

Distributed Systems Recitation 1. Tamim Jabban

Inheritance (Outsource: )

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Object-Oriented Programming in the Java language

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

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

Inheritance and Polymorphism

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Programming Languages and Techniques (CIS120)

Java Magistère BFA

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

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

AP CS Unit 6: Inheritance Notes

Programming Languages and Techniques (CIS120)

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

COMP 250: Java Object Oriented Programming

INHERITANCE. Spring 2019

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

C12a: The Object Superclass and Selected Methods

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

Equality. Michael Ernst. CSE 331 University of Washington

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

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

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

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

Distributed Systems Recitation 1. Tamim Jabban

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

Equality. Michael Ernst. CSE 331 University of Washington

Class, Variable, Constructor, Object, Method Questions

Java Classes, Inheritance, and Interfaces

c) And last but not least, there are javadoc comments. See Weiss.

Practice for Chapter 11

COMP 250. Lecture 26. maps. Nov. 8/9, 2017

CMSC 132: Object-Oriented Programming II

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

Accelerating Information Technology Innovation

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Solutions to Sample JAC444 Midterm Test

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Object Oriented Programming: Based on slides from Skrien Chapter 2

COMP 110/L Lecture 20. Kyle Dewey

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

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

Programming Languages and Techniques (CIS120)

The Liskov Substitution Principle

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

INHERITANCE AND EXTENDING CLASSES

Overriding methods. Changing what we have inherited from e.g. Object

Inheritance (Part 2) Notes Chapter 6

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

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

ITI Introduction to Computing II

CS 251 Intermediate Programming Inheritance

Building custom components IAT351

Computer Science II (20073) Week 1: Review and Inheritance

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

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

Lesson 8 Passing Parameters. By John B. Owen All rights reserved 2011, revised 2014

ITI Introduction to Computing II

Rules and syntax for inheritance. The boring stuff

Outline. More optimizations for our interpreter. Types for objects

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

CS260 Intro to Java & Android 03.Java Language Basics

Programming Languages and Techniques (CIS120e)

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Transcription:

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

All dogs are animals. All beagles are dogs. relationships between classes 2

All dogs are animals. All beagles are dogs. relationships between classes Animals are born (and have a birthdate). Dogs bark. class definitions Beagles chase rabbits. 3

Inheritance class Animal Date birth Date death Place home void eat() extends class Dog String serialnumber Person owner void bark() class Beagle hunt () extends 4

Inheritance extends class Poodle void show() class Dog String serialnumber Person owner void bark() extends class Beagle void hunt () extends class Doberman void fight () e.g. Beagle is a subclass of Dog. Dog is a superclass of Beagle. A subclass inherits the fields and methods of its superclass. 5

Constructors are not inherited. extends class Poodle Poodle() show() class Dog String serialnumber Person owner Dog() void bark() class Beagle Beagle() hunt () extends extends class Doberman Doberman() fight () Each object belongs to a unique class. 6

class Animal { Place home; Constructor chaining Animal( ) {... } } Animal( Place home) { this.home = home; } class Dog extends Animal { String owner; Dog( ) { } // This constructor automatically creates // fields that are inherited from the superclass } 7

class Animal { Place home; Animal() {... } Constructor chaining (a few details ) } Animal( Place home) { this.home = home; } class Dog extends Animal { String owner; Dog() { } // This constructor automatically calls super() which creates // fields that are inherited from the superclass } Dog(Place home, String owner) { super(home); // Here we need to explicitly write it. this.owner = owner; } 8

Sometimes we have two versions of a method (method) overloading vs. (method) overriding Today we will see some examples. The reasons why we do this will hopefully become more clear over the next few lectures. 9

Example of overloading LinkedList<E> void add( E e) void add( int index, E e ) E remove( int index) E remove( ) // removes head 10

Overloading same method name, but different parameter types (i.e. different method signature note signature does not include the return type) within a class, or between a class and its superclass Example on previous slide was within a class 11

Overriding subclass method overrides a superclass method same method signatures (i.e. same method name and parameter types) 12

Overriding e.g. bark() extends class Poodle void show() void bark() class Dog String serialnumber Person owner void bark() {print woof } extends class Beagle void hunt () void bark() extends class Doberman void fight () void bark() {print arw } https//www.youtu be.com/watch?v=_ wqk15etcmo {print aowwwuuu } https//www.youtub e.com/watch?v=esje c0jwexu {print Arh! Arh! Arh! } https//www.youtube.c om/watch?v=s5y- Gyt57Dw 13

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object class extends (automatic) class Animal extends class Dog extends class Beagle 14

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 15

Object.equals( Object ) Object obj1, obj2 obj1.equals( obj2 ) is equivalent to obj1 == obj2 16

https//docs.oracle.com/javase/7/docs/api/java/lang/object.html see MATH 240 17

Object.equals( Object ) x.equals(x) should always return true x.equals(y) should return true if and only if y.equals(x) returns true if x.equals(y) and y.equals(z) both return true, Then x.equals(z) should return true x.equals(null) should return false. The above rules should hold for non-null references. 18

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class Animal Animal( ) boolean equals( Object ) Animal.equals( Object) This is overriding. 19

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class Animal Animal( ) boolean equals( Animal ) Animal.equals( Animal ) This is overloading. 20

overloading vs. overriding I will say a bit more about when we use one versus the other over the next few lectures. 21

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class String String( ) String.equals( Object ) boolean equals( Object ) int hashcode( ) This is overriding. 22

https//docs.oracle.com/javase/7/docs/api/java/lang/string.html String.equals( Object ) 23

You should Compare strings using String.equals( Object ) rather than == to avoid nasty surprises. String s1 = "sur"; String s2 = "surprise"; System.out.println(("sur" + "prise") == "surprise"); System.out.println("sur" == s1); System.out.println(("surprise" == "surprise")); System.out.println("surprise" == new String("surprise")); System.out.println((s1 + "prise") == "surprise"); System.out.println((s1 + "prise") == s2); System.out.println((s1 + "prise").equals("surprise")); System.out.println((s1 + "prise").equals(s2)); System.out.println( s2.equals(s1 + "prise")); // true // true // true // false // false // false // true // true // true 24

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.equals( Object ) extends (automatic) class LinkedList LinkedList( ) boolean equals( Object ) LinkedList.equals( Object ) This is overriding. 25

https//docs.oracle.com/javase/7/docs/api/java/util/list.html LinkedList.equals( Object ) List interface next lecture 26

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 27

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.hashCode() Returns a 32 bit integer extends (automatic) class String String( ) boolean equals( String ) int hashcode( ) String tostring( ) Object clone( ) String.hashCode() This is overriding. 28

SLIDE ADDED Nov. 20 (I will discuss this next lecture too) Java API for Object.hashCode() recommends If o1.equals(o2) is true then o1.hashcode() == o2.hashcode() should be true. The converse need not hold. It can easily happen that two objects have the same hashcode but the objects are not considered equal. 29

https//docs.oracle.com/javase/7/docs/api/java/lang/string.html String.hashCode() 30

For fun, check out hashcode() method for other classes e.g. LinkedList. 31

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) 32

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns? extends (automatic) class Animal Animal( ) boolean equals( Animal ) int hashcode( ) String tostring( ) Animal.toString() This is overriding. Returns? 33

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns classname + @ + hashcode() extends (automatic) class Animal Animal( ) boolean equals( Animal ) int hashcode( ) String tostring( ) Animal.toString() This is overriding. Returns. however you define it 34

Object.toString() returns classname + @ + hashcode() In order to explain this, I need to take a detour. I have also added the following slides to lecture 2 (binary numbers). That is really where the following material belongs. 35

As you know from Assignment 1, we can write any positive integer m uniquely as a sum of powers of any number called the base (or radix). m = N 1 i=0 a i (base) i The coefficients a i are in {0, 1,.., base 1} We write (a N 1 a N 2 a N 3 a 2 a 1 a 0 ) base Humans usually use base 10. Computers use base 2. 36

e.g. Hexadecimal (base 16) m = N 1 i=0 a i (16) i The coefficients a i are in {0, 1,.., 10, 11, 15} Instead we use a i in {0, 1,.., 8, 9, a, b, c, d, e, f} 37

38 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 a b c d e f Hexadecimal

Common use of hexadecimal representing long bit strings Example 0010 1111 1010 0011 2 f a 3 Example 2 10 1100 We write 2c (10 1100), not b0 (1011 00). 39

Object.toString() Returns classname + @ + hashcode() In Eclipse, we get the package name also. 32 bit integer (8 hexadecimal digits) Address of object 40

Object.toString() System.out.println( new Object() ); What does this print? 41

Object.toString() System.out.println( new Object() ); What does this print? java.lang.object@7852e922 32 bit integer represented in hexadecimal. You ll get a different number if you run it again. 42

Object.toString() Object o = new Object(); System.out.println( o ); What does this print? java.lang.object@7852e922 package + class name 32 bit integer represented in hexadecimal. You ll get a different number if you run it again. 43

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Object.toString() Returns classname + @ + hashcode() class String String( ) boolean equals( Object ) int hashcode( ) String tostring( ) String.toString() This is overriding. Returns itself! 44