Skip to content

Commit 0591c12

Browse files
committed
more specific inheritance example
1 parent 21f5153 commit 0591c12

1 file changed

Lines changed: 84 additions & 29 deletions

File tree

‎koans/src/beginner/AboutInheritance.java‎

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,110 @@
22

33
import com.sandwich.koan.Koan;
44

5-
import java.util.Collection;
6-
import java.util.Collections;
7-
import java.util.List;
8-
95
import static com.sandwich.koan.constant.KoanConstants.__;
106
import static com.sandwich.util.Assert.assertEquals;
117

128
public class AboutInheritance {
139

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!";
1718
}
1819
}
1920

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!";
2325
}
2426

25-
public String doStuff(String s) {
26-
return s;
27+
public boolean canFetch() {
28+
return true;
2729
}
2830
}
2931

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+
}
3540
}
3641

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?
3954
}
4055

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+
}
4567

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, __);
4778
}
4879

4980
@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, __);
5588
}
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+
// }
56111
}

0 commit comments

Comments
 (0)