|
2 | 2 |
|
3 | 3 | import com.sandwich.koan.Koan; |
4 | 4 |
|
5 | | -import java.util.Collection; |
6 | | -import java.util.Collections; |
7 | | -import java.util.List; |
8 | | - |
9 | 5 | import static com.sandwich.koan.constant.KoanConstants.__; |
10 | 6 | import static com.sandwich.util.Assert.assertEquals; |
11 | 7 |
|
12 | 8 | public class AboutInheritance { |
13 | 9 |
|
14 | | - class Parent { |
15 | | - public String doStuff() { |
16 | | - return "parent"; |
| 10 | + abstract class Animal { |
| 11 | + abstract public String makeSomeNoise(); |
| 12 | + } |
| 13 | + |
| 14 | + class Cow extends Animal { |
| 15 | + @Override |
| 16 | + public String makeSomeNoise() { |
| 17 | + return "Moo!"; |
17 | 18 | } |
18 | 19 | } |
19 | 20 |
|
20 | | - class Child extends Parent { |
21 | | - public String doStuff() { |
22 | | - return "child"; |
| 21 | + class Dog extends Animal { |
| 22 | + @Override |
| 23 | + public String makeSomeNoise() { |
| 24 | + return "Woof!"; |
23 | 25 | } |
24 | 26 |
|
25 | | - public String doStuff(String s) { |
26 | | - return s; |
| 27 | + public boolean canFetch() { |
| 28 | + return true; |
27 | 29 | } |
28 | 30 | } |
29 | 31 |
|
30 | | - @Koan |
31 | | - public void differenceBetweenOverloadingAndOverriding() { |
32 | | - assertEquals(new Parent().doStuff(), __); |
33 | | - assertEquals(new Child().doStuff(), __); |
34 | | - assertEquals(new Child().doStuff("oh no"), __); |
| 32 | + class Puppy extends Dog { |
| 33 | + @Override |
| 34 | + public String makeSomeNoise() { |
| 35 | + return "Squeak!"; |
| 36 | + } |
| 37 | + public boolean canFetch() { |
| 38 | + return false; |
| 39 | + } |
35 | 40 | } |
36 | 41 |
|
37 | | - abstract class ParentTwo { |
38 | | - abstract public Collection<?> doStuff(); |
| 42 | + @Koan |
| 43 | + public void methodOverloading() { |
| 44 | + Cow bob = new Cow(); |
| 45 | + Dog max = new Dog(); |
| 46 | + Puppy barney = new Puppy(); |
| 47 | + assertEquals(bob.makeSomeNoise(), __); |
| 48 | + assertEquals(max.makeSomeNoise(), __); |
| 49 | + assertEquals(barney.makeSomeNoise(), __); |
| 50 | + |
| 51 | + assertEquals(max.canFetch(), __); |
| 52 | + assertEquals(barney.canFetch(), __); |
| 53 | + // but can Bob the Cow fetch? |
39 | 54 | } |
40 | 55 |
|
41 | | - class ChildTwo extends ParentTwo { |
42 | | - public Collection<?> doStuff() { |
43 | | - return Collections.emptyList(); |
44 | | - } |
| 56 | + @Koan |
| 57 | + public void methodOverloadingUsingPolymorphism() { |
| 58 | + Animal bob = new Cow(); |
| 59 | + Animal max = new Dog(); |
| 60 | + Animal barney = new Puppy(); |
| 61 | + assertEquals(bob.makeSomeNoise(), __); |
| 62 | + assertEquals(max.makeSomeNoise(), __); |
| 63 | + assertEquals(barney.makeSomeNoise(), __); |
| 64 | + // but can max or barney (here as an Animal) fetch? |
| 65 | + // try to write it down here |
| 66 | + } |
45 | 67 |
|
46 | | - ; |
| 68 | + @Koan |
| 69 | + public void inheritanceHierarchy() { |
| 70 | + Animal someAnimal = new Cow(); |
| 71 | + Animal bob = new Cow(); |
| 72 | + assertEquals(someAnimal.makeSomeNoise().equals(bob.makeSomeNoise()), __); |
| 73 | + // cow is a Cow, but it can also be an animal |
| 74 | + assertEquals(bob instanceof Animal, __); |
| 75 | + assertEquals(bob instanceof Cow, __); |
| 76 | + // but is it a Puppy? |
| 77 | + assertEquals(bob instanceof Puppy, __); |
47 | 78 | } |
48 | 79 |
|
49 | 80 | @Koan |
50 | | - public void overriddenMethodsMayReturnSubtype() { |
51 | | - // What do you need to change in order to get rid of the type cast? |
52 | | - // Why does this work? |
53 | | - List<?> list = (List<?>) new ChildTwo().doStuff(); |
54 | | - assertEquals(list instanceof List, __); |
| 81 | + public void deeperInheritanceHierarchy() { |
| 82 | + Dog max = new Dog(); |
| 83 | + Puppy barney = new Puppy(); |
| 84 | + assertEquals(max instanceof Puppy, __); |
| 85 | + assertEquals(max instanceof Dog, __); |
| 86 | + assertEquals(barney instanceof Puppy, __); |
| 87 | + assertEquals(barney instanceof Dog, __); |
55 | 88 | } |
| 89 | + |
| 90 | + // TODO overriding |
| 91 | +// |
| 92 | +// abstract class ParentTwo { |
| 93 | +// abstract public Collection<?> doStuff(); |
| 94 | +// } |
| 95 | +// |
| 96 | +// class ChildTwo extends ParentTwo { |
| 97 | +// public Collection<?> doStuff() { |
| 98 | +// return Collections.emptyList(); |
| 99 | +// } |
| 100 | +// |
| 101 | +// ; |
| 102 | +// } |
| 103 | +// |
| 104 | +// @Koan |
| 105 | +// public void overriddenMethodsMayReturnSubtype() { |
| 106 | +// // What do you need to change in order to get rid of the type cast? |
| 107 | +// // Why does this work? |
| 108 | +// List<?> list = (List<?>) new ChildTwo().doStuff(); |
| 109 | +// assertEquals(list instanceof List, __); |
| 110 | +// } |
56 | 111 | } |
0 commit comments