+ All Categories
Home > Documents > Inheritance Slides - McGill University

Inheritance Slides - McGill University

Date post: 07-Dec-2021
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
44
1 COMP 250 Lecture 30 inheritance overriding vs overloading Nov. 17, 2017
Transcript

1

COMP 250

Lecture 30

inheritance

overriding vs overloading

Nov. 17, 2017

All dogs are animals.

All beagles are dogs.

relationships between classes

2

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

class AnimalDate birthDate deathPlace homevoid eat()

:

class DogString serialNumberPerson owner

void bark():

class Beaglehunt ()

:

Inheritance

extends

extends

4

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

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

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

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

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

Example of overloadingLinkedList<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

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

class Animal:

class Dog:

class Beagle:

Object class

extends

extends

class Object

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

:

extends (automatic)

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

17

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

see MATH 240

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.

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

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

overloadingvs.

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( )

:

extends (automatic)

class String

String( )

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

String.equals( Object )

22

Object.equals( Object )

23

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

String.equals( Object )

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.

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

26

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

LinkedList.equals( Object ) List interface: next lecture

27

class Object

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

:

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()

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.

30

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

String.hashCode()

31

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

32

class Object

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

:

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()

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()

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()

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.

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}

38

Binary

0000000100100011010001010110011110001001101010111100110111101111

Decimal

0123456789101112131415

0123456789abcdef

Hexadecimal

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).

Object.toString()

Returns classname + “@” + hashCode()

32 bit integer(8 hexadecimal digits)

Address of object

In Eclipse, we get the package name also.

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

32 bit integer represented in hexadecimal.You’ll get a different number if you run it again.

package + class name

43

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()


Recommended