1
COMP 250
Lecture 30
inheritance
- verriding vs overloading
- Nov. 17, 2017
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
1
relationships between classes
2
relationships between classes class definitions
3
class Animal Date birth Date death Place home void eat() : class Dog String serialNumber Person owner void bark() : class Beagle hunt () :
extends extends
4
class Dog String serialNumber Person owner void bark() : class Beagle void hunt () :
extends class Doberman void fight () : extends class Poodle void show() : extends
class Dog String serialNumber Person owner Dog() void bark() : class Beagle Beagle() hunt () :
extends class Doberman Doberman() fight () : extends class Poodle Poodle() show() : extends
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 }
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; } : }
8
9
10
11
12
class Dog String serialNumber Person owner void bark() : class Beagle void hunt () void 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
Gyt57Dw
13
class Animal : class Dog : class Beagle :
extends extends
extends (automatic)
14
15
16
17
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
18
extends (automatic)
19
extends (automatic)
20
21
extends (automatic)
22
23
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
24
extends (automatic)
25
26
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
List interface: next lecture
27
extends (automatic)
28
29
30
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
31
32
extends (automatic)
33
extends (automatic)
34
35
36
37
38
39
40
41
32 bit integer represented in hexadecimal. You’ll get a different number if you run it again.
42
32 bit integer represented in hexadecimal. You’ll get a different number if you run it again. package + class name
43
44