inheritance overriding vs overloading Nov. 17, 2017 1 All dogs - - PowerPoint PPT Presentation

inheritance
SMART_READER_LITE
LIVE PREVIEW

inheritance overriding vs overloading Nov. 17, 2017 1 All dogs - - PowerPoint PPT Presentation

COMP 250 Lecture 30 inheritance overriding vs overloading Nov. 17, 2017 1 All dogs are animals. relationships between All beagles are dogs. classes 2 All dogs are animals. relationships between All beagles are dogs. classes Animals


slide-1
SLIDE 1

1

COMP 250

Lecture 30

inheritance

  • verriding vs overloading
  • Nov. 17, 2017
slide-2
SLIDE 2

All dogs are animals. All beagles are dogs.

relationships between classes

2

slide-3
SLIDE 3

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

slide-4
SLIDE 4

class Animal Date birth Date death Place home void eat() : class Dog String serialNumber Person owner void bark() : class Beagle hunt () :

Inheritance

extends extends

4

slide-5
SLIDE 5

class Dog String serialNumber Person owner void bark() : class Beagle void hunt () :

Inheritance

extends class Doberman void fight () : extends class Poodle void 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

slide-6
SLIDE 6

class Dog String serialNumber Person owner Dog() void bark() : class Beagle Beagle() hunt () :

Constructors are not inherited.

extends class Doberman Doberman() fight () : extends class Poodle Poodle() show() : extends

Each object belongs to a unique class.

6

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Example of overloading

LinkedList<E>

void add( E e) void add( int index, E e ) E remove( int index) E remove( ) // removes head

10

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Overriding

  • subclass method overrides a superclass method
  • same method signatures

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

12

slide-13
SLIDE 13

class Dog String serialNumber Person owner void bark() : class Beagle void hunt () void bark()

Overriding e.g. bark()

extends class Doberman void fight () void bark() extends class Poodle void show() void bark() extends {print “arw”} https://www.youtu be.com/watch?v=_ wqK15EtCMo {print “woof”} {print “aowwwuuu”} https://www.youtub e.com/watch?v=esje c0JWEXU {print “Arh! Arh! Arh!”} https://www.youtube.c

  • m/watch?v=s5Y-

Gyt57Dw

13

slide-14
SLIDE 14

class Animal : class Dog : class Beagle :

Object class

extends extends

class Object

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

extends (automatic)

14

slide-15
SLIDE 15

class Object

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

15

slide-16
SLIDE 16

Object.equals( Object )

Object obj1, obj2

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

16

slide-17
SLIDE 17

17

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

see MATH 240

slide-18
SLIDE 18

Object.equals( Object )

18

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.

slide-19
SLIDE 19

class Object

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

extends (automatic)

class Animal

Animal( ) : boolean equals( Object ) : :

Object.equals( Object ) Animal.equals( Object) This is overriding.

19

slide-20
SLIDE 20

class Object

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

extends (automatic)

class Animal

Animal( ) : boolean equals( Animal ) : :

Object.equals( Object ) Animal.equals( Animal ) This is overloading.

20

slide-21
SLIDE 21
  • verloading

vs.

  • verriding

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

21

slide-22
SLIDE 22

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 )

slide-23
SLIDE 23

23

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

String.equals( Object )

slide-24
SLIDE 24

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

24

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

slide-25
SLIDE 25

class Object

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

extends (automatic)

class LinkedList

LinkedList( ) : boolean equals( Object ) : :

Object.equals( Object ) LinkedList.equals( Object ) This is overriding.

25

slide-26
SLIDE 26

26

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

LinkedList.equals( Object )

List interface: next lecture

slide-27
SLIDE 27

27

class Object

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

slide-28
SLIDE 28

class Object

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

extends (automatic)

class String

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

This is overriding.  Returns a 32 bit integer

28

Object.hashCode() String.hashCode()

slide-29
SLIDE 29

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

  • 1.hashCode() == o2.hashCode() should be true.

The converse need not hold. It can easily happen that two

  • bjects have the same hashCode but the objects are not

considered equal.

slide-30
SLIDE 30

30

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

String.hashCode()

slide-31
SLIDE 31

31

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

slide-32
SLIDE 32

32

class Object

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

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 it

34

Object.toString() This is overriding. Animal.toString()

slide-35
SLIDE 35

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

slide-36
SLIDE 36

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.

slide-37
SLIDE 37

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}

slide-38
SLIDE 38

38

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

slide-39
SLIDE 39

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

slide-40
SLIDE 40

Object.toString()

Returns classname + “@” + hashCode()

32 bit integer (8 hexadecimal digits) Address of object In Eclipse, we get the package name also.

40

slide-41
SLIDE 41

Object.toString()

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

41

slide-42
SLIDE 42

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

slide-43
SLIDE 43

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

slide-44
SLIDE 44

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