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

Inheritance Slides - McGill University

Dec 07, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inheritance Slides - McGill University

1

COMP 250

Lecture 30

inheritance

overriding vs overloading

Nov. 17, 2017

Page 2: Inheritance Slides - McGill University

All dogs are animals.

All beagles are dogs.

relationships between classes

2

Page 3: Inheritance Slides - McGill University

All dogs are animals.

All beagles are dogs.

Animals are born (and have a birthdate).

Dogs bark.

Beagles chase rabbits.

relationships between classes

class definitions

3

Page 4: Inheritance Slides - McGill University

class AnimalDate birthDate deathPlace homevoid eat()

:

class DogString serialNumberPerson owner

void bark():

class Beaglehunt ()

:

Inheritance

extends

extends

4

Page 5: Inheritance Slides - McGill University

class DogString serialNumberPerson owner

void bark():

class Beaglevoid hunt ()

:

Inheritance

extends

class Dobermanvoid fight ()

:

extends

class Poodlevoid show()

:

extends

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

Page 6: Inheritance Slides - McGill University

class DogString serialNumberPerson owner

Dog()void bark()

:

class BeagleBeagle()hunt ()

:

Constructors are not inherited.

extends

class DobermanDoberman()

fight ():

extends

class PoodlePoodle()show()

:

extends

Each object belongs to a unique class. 6

Page 7: Inheritance Slides - McGill University

class Animal {Place home;

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

}

Constructor chaining

7

Page 8: Inheritance Slides - McGill University

class Animal {Place home;

Animal() { ... }

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;

}:

}

Constructor chaining(a few details…)

8

Page 9: Inheritance Slides - McGill University

Sometimes we have two versions of a method:

(method) overloadingvs.

(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

Page 10: Inheritance Slides - McGill University

Example of overloadingLinkedList<E>

void add( E e)

void add( int index, E e )

E remove( int index)

E remove( ) // removes head

10

Page 11: Inheritance Slides - McGill University

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

Page 12: Inheritance Slides - McGill University

Overriding

• subclass method overrides a superclass method

• same method signatures

(i.e. same method name and parameter types)

12

Page 13: Inheritance Slides - McGill University

class DogString serialNumberPerson owner

void bark():

class Beaglevoid hunt ()void bark()

Overriding e.g. bark()

extends

class Dobermanvoid fight ()void bark()

extends

class Poodlevoid show()void bark()

extends

{print “arw”}

https://www.youtube.com/watch?v=_wqK15EtCMo

{print “woof”}

{print “aowwwuuu”}

https://www.youtube.com/watch?v=esjec0JWEXU

{print “Arh! Arh! Arh!”}

https://www.youtube.com/watch?v=s5Y-Gyt57Dw

13

Page 14: Inheritance Slides - McGill University

class Animal:

class Dog:

class Beagle:

Object class

extends

extends

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

14

Page 15: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

15

Page 16: Inheritance Slides - McGill University

Object.equals( Object )

Object obj1, obj2

obj1.equals( obj2 ) is equivalent to obj1 == obj2

16

Page 17: Inheritance Slides - McGill University

17

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

see MATH 240

Page 18: Inheritance Slides - McGill University

Object.equals( Object )

18

x.equals(x) should always return true

x.equals(y) should return true if and only ify.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.

Page 19: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class AnimalAnimal( )

: boolean equals( Object )

::

Object.equals( Object )

Animal.equals( Object)

This is overriding.

19

Page 20: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class AnimalAnimal( )

: boolean equals( Animal )

::

Object.equals( Object )

Animal.equals( Animal )

This is overloading.

20

Page 21: Inheritance Slides - McGill University

overloadingvs.

overriding

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

21

Page 22: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class String

String( )

boolean equals( Object )int hashCode( ) This is overriding.

String.equals( Object )

22

Object.equals( Object )

Page 23: Inheritance Slides - McGill University

23

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

String.equals( Object )

Page 24: Inheritance Slides - McGill University

String s1 = "sur"; String s2 = "surprise";

System.out.println(("sur" + "prise") == "surprise"); // trueSystem.out.println("sur" == s1); // trueSystem.out.println(("surprise" == "surprise")); // trueSystem.out.println("surprise" == new String("surprise")); // falseSystem.out.println((s1 + "prise") == "surprise"); // falseSystem.out.println((s1 + "prise") == s2); // false

System.out.println((s1 + "prise").equals("surprise")); // trueSystem.out.println((s1 + "prise").equals(s2)); // trueSystem.out.println( s2.equals(s1 + "prise")); // true

24

You should Compare strings using String.equals( Object ) rather than “==“ to avoid nasty surprises.

Page 25: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class LinkedListLinkedList( )

: boolean equals( Object )

::

Object.equals( Object )

LinkedList.equals( Object )

This is overriding.

25

Page 26: Inheritance Slides - McGill University

26

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

LinkedList.equals( Object ) List interface: next lecture

Page 27: Inheritance Slides - McGill University

27

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

Page 28: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class StringString( )

boolean equals( String )int hashCode( )String toString( )Object clone( )

This is overriding.

Returns a 32 bit integer

28

Object.hashCode()

String.hashCode()

Page 29: Inheritance Slides - McGill University

SLIDE ADDED Nov. 20(I will discuss this next lecture too)

29

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.

Page 30: Inheritance Slides - McGill University

30

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

String.hashCode()

Page 31: Inheritance Slides - McGill University

31

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

Page 32: Inheritance Slides - McGill University

32

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

Page 33: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class Animal

Animal( )boolean equals( Animal )int hashCode( )String toString( )

This is overriding.

Returns ?

Returns ?

33

Object.toString()

Animal.toString()

Page 34: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

extends (automatic)

class Animal

Animal( )boolean equals( Animal )int hashCode( )String toString( )

:

Returns classname + “@” + hashCode()

Returns …. however you define it34

Object.toString()

This is overriding.

Animal.toString()

Page 35: Inheritance Slides - McGill University

35

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.

Object.toString()

Page 36: Inheritance Slides - McGill University

36

As you know from Assignment 1, we can write any positive integer 𝑚 uniquely as a sum of powers of any number called the base (or radix).

𝑚 =

𝑖=0

𝑁−1

𝑎𝑖 (𝑏𝑎𝑠𝑒)𝑖

The coefficients 𝑎𝑖 are in {0, 1, ….., 𝑏𝑎𝑠𝑒 – 1}

We write (𝑎𝑁−1 𝑎𝑁−2 𝑎𝑁−3 … 𝑎2 𝑎1 𝑎0 )𝑏𝑎𝑠𝑒

Humans usually use base 10. Computers use base 2.

Page 37: Inheritance Slides - McGill University

37

e.g. Hexadecimal (base 16)

𝑚 =

𝑖=0

𝑁−1

𝑎𝑖 (16)𝑖

The coefficients 𝑎𝑖 are in {0, 1, ….., 10, 11, … 15}

Instead we use 𝑎𝑖 in {0, 1, ….., 8, 9, a, b, c, d, e, f}

Page 38: Inheritance Slides - McGill University

38

Binary

0000000100100011010001010110011110001001101010111100110111101111

Decimal

0123456789101112131415

0123456789abcdef

Hexadecimal

Page 39: Inheritance Slides - McGill University

Common use of hexadecimal: representing long bit strings

39

Example: 0010 1111 1010 0011

2 f a 3

Example 2: 10 1100

We write 2c (10 1100), not b0 (1011 00).

Page 40: Inheritance Slides - McGill University

Object.toString()

Returns classname + “@” + hashCode()

32 bit integer(8 hexadecimal digits)

Address of object

In Eclipse, we get the package name also.

40

Page 41: Inheritance Slides - McGill University

Object.toString()

System.out.println( new Object() );

What does this print?

41

Page 42: Inheritance Slides - McGill University

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

Page 43: Inheritance Slides - McGill University

Object.toString()

Object o = new Object();

System.out.println( o );

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.

package + class name

43

Page 44: Inheritance Slides - McGill University

class Object

boolean equals( Object )int hashCode( )String toString( )Object clone( )

:

class String

String( )boolean equals( Object )int hashCode( )String toString( )

:

Returns classname + “@” + hashCode()

Returns itself!44

This is overriding.

Object.toString()

String.toString()