| 1 |
// name: super-4-multilevel.ck |
| 2 |
// desc: more comprehensive usage example of `super` keyword |
| 3 |
// across multiple levels of inheritance |
| 4 |
// author: Niccolo, Fall 2025 |
| 5 |
|
| 6 |
class Base |
| 7 |
{ |
| 8 |
fun Base() |
| 9 |
{ |
| 10 |
<<< "Base ctor" >>>; |
| 11 |
} |
| 12 |
|
| 13 |
fun void overridden() |
| 14 |
{ |
| 15 |
<<< "Base overridden" >>>; |
| 16 |
} |
| 17 |
|
| 18 |
fun void not_overridden() |
| 19 |
{ |
| 20 |
<<< "Base not_overridden" >>>; |
| 21 |
} |
| 22 |
|
| 23 |
fun void not_overridden_by_child() |
| 24 |
{ |
| 25 |
<<< "Base not_overridden_by_child" >>>; |
| 26 |
} |
| 27 |
|
| 28 |
fun void bubble_up() |
| 29 |
{ |
| 30 |
<<< "Base bubble_up" >>>; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
class Parent extends Base |
| 35 |
{ |
| 36 |
fun Parent() |
| 37 |
{ |
| 38 |
<<< "Parent ctor" >>>; |
| 39 |
} |
| 40 |
|
| 41 |
fun void overridden() |
| 42 |
{ |
| 43 |
<<< "Parent overridden" >>>; |
| 44 |
} |
| 45 |
|
| 46 |
fun void not_overridden_by_child() |
| 47 |
{ |
| 48 |
<<< "Parent not_overridden_by_child" >>>; |
| 49 |
} |
| 50 |
|
| 51 |
fun void bubble_up() |
| 52 |
{ |
| 53 |
<<< "Parent bubble_up" >>>; |
| 54 |
super.bubble_up(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
class Child extends Parent |
| 59 |
{ |
| 60 |
fun Child() |
| 61 |
{ |
| 62 |
<<< "Child ctor" >>>; |
| 63 |
} |
| 64 |
|
| 65 |
fun void test() |
| 66 |
{ |
| 67 |
<<< "test start" >>>; |
| 68 |
|
| 69 |
// call parent contructor - seems to be allowed by ChucK so it should work with super as well |
| 70 |
<<< "super.Parent() - expecting parent ctor" >>>; |
| 71 |
super.Parent(); |
| 72 |
// should be allowed by same logic |
| 73 |
<<< "super.Base() - expecting base ctor" >>>; |
| 74 |
super.Base(); |
| 75 |
// should call base version of not_overridden (since no parent version exists / the base version is the parent version) |
| 76 |
<<< "super.not_overridden() - expecting base not_overridden" >>>; |
| 77 |
super.not_overridden(); |
| 78 |
// should call parent version of overridden |
| 79 |
<<< "super.overridden() - expecting parent overridden" >>>; |
| 80 |
super.overridden(); |
| 81 |
// should call child version of overridden |
| 82 |
<<< "this.overridden() - expecting child overridden" >>>; |
| 83 |
this.overridden(); |
| 84 |
// should call parent version of function, not base version |
| 85 |
<<< "super.not_overridden_by_child() - expecting parent not_overridden_by_child" >>>; |
| 86 |
super.not_overridden_by_child(); |
| 87 |
// should call child version of function, which will then bubble up to the base |
| 88 |
<<< "this.bubble_up() - expecting child bubble_up / parent bubble_up / base bubble_up" >>>; |
| 89 |
this.bubble_up(); |
| 90 |
|
| 91 |
<<< "test complete" >>>; |
| 92 |
} |
| 93 |
|
| 94 |
fun void overridden() |
| 95 |
{ |
| 96 |
<<< "Child overridden" >>>; |
| 97 |
} |
| 98 |
|
| 99 |
fun void not_override() |
| 100 |
{ |
| 101 |
<<< "Child not_override" >>>; |
| 102 |
} |
| 103 |
|
| 104 |
fun void bubble_up() |
| 105 |
{ |
| 106 |
<<< "Child bubble_up" >>>; |
| 107 |
super.bubble_up(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
Child child; |
| 112 |
child.test(); |