forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutInheritance.java
More file actions
44 lines (35 loc) · 1.1 KB
/
AboutInheritance.java
File metadata and controls
44 lines (35 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package beginner;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.sandwich.koan.Koan;
public class AboutInheritance {
class Parent {
public String doStuff() { return "parent"; }
}
class Child extends Parent {
public String doStuff() { return "child"; }
public String doStuff(String s) { return s; }
}
@Koan
public void differenceBetweenOverloadingAndOverriding() {
assertEquals(new Parent().doStuff(), __);
assertEquals(new Child().doStuff(), __);
assertEquals(new Child().doStuff("oh no"), __);
}
abstract class ParentTwo {
abstract public Collection<?> doStuff();
}
class ChildTwo extends ParentTwo {
public Collection<?> doStuff() { return Collections.emptyList(); };
}
@Koan
public void overriddenMethodsMayReturnSubtype() {
// What do you need to change in order to get rid of the type cast?
// Why does this work?
List<?> list = (List<?>) new ChildTwo().doStuff();
assertEquals(list instanceof List, __);
}
}