Skip to content

Commit 398c92d

Browse files
author
David Reed
committed
Inverting inheritance tree in AboutCasting to make it less confusing
1 parent 557e507 commit 398c92d

1 file changed

Lines changed: 88 additions & 85 deletions

File tree

‎koans/src/beginner/AboutCasting.java‎

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
@SuppressWarnings("unused")
1010
public class AboutCasting {
11-
11+
1212
@Koan
1313
public void longPlusInt() {
1414
int a = 6;
@@ -18,96 +18,99 @@ public void longPlusInt() {
1818
assertEquals(c instanceof Integer, __);
1919
assertEquals(c instanceof Long, __);
2020
}
21-
22-
@Koan
21+
22+
@Koan
2323
public void forceIntTypecast() {
24-
long a = 2147483648L;
24+
long a = 2147483648L;
2525
// What happens if we force a long value into an int?
26-
int b = (int)a;
26+
int b = (int) a;
2727
assertEquals(b, __);
2828
}
29-
30-
@Koan
29+
30+
@Koan
3131
public void implicitTypecast() {
32-
int a = 1;
33-
int b = Integer.MAX_VALUE;
32+
int a = 1;
33+
int b = Integer.MAX_VALUE;
3434
long c = a + b; // still overflows int... which is the Integer.MIN_VALUE, the operation occurs prior to assignment to long
3535
assertEquals(c, __);
3636
}
37-
38-
interface Sleepable {
39-
String sleep();
40-
}
41-
class Child implements Sleepable{
42-
public String sleep() {
43-
return "zzzz";
44-
}
45-
}
46-
class Parent extends Child {
47-
public String complain() {
48-
return "TPS reports don't even have a cover letter!";
49-
}
50-
}
51-
class GrandParent extends Parent {
52-
public String complain() {
53-
return "Get your feet off the davenport!";
54-
}
55-
}
56-
57-
@Koan
58-
public void downCastWithInerhitance() {
59-
GrandParent grandParent = new GrandParent();
60-
Parent parentReference = grandParent; // Why isn't there an explicit cast?
61-
assertEquals(grandParent instanceof GrandParent, __);
62-
assertEquals(parentReference instanceof GrandParent, __);
63-
assertEquals(parentReference instanceof Parent, __);
64-
assertEquals(parentReference instanceof Child, __);
65-
}
66-
67-
@Koan
68-
public void downCastAndPolymophism() {
69-
GrandParent grandParent = new GrandParent();
70-
Parent parentReference = grandParent;
71-
// If the result is unexpected, consider the difference between an instance and its reference
72-
assertEquals(parentReference.complain(), __);
73-
}
74-
75-
@Koan
76-
public void upCastWithInheritance() {
77-
Child grandParent = new GrandParent();
78-
Parent parentReference = (Parent)grandParent; // Why do we need an explicit cast here?
79-
GrandParent grandParentReference = (GrandParent)parentReference; // Or here?
80-
assertEquals(grandParentReference instanceof GrandParent,__);
81-
assertEquals(grandParentReference instanceof Parent,__);
82-
assertEquals(grandParentReference instanceof Child,__);
83-
}
84-
85-
@Koan
86-
public void upCastAndPolymophism() {
87-
Child grandParent = new GrandParent();
88-
Parent parent = (GrandParent)grandParent;
89-
// Think about the result. Did you expect that? Why?
90-
// How is that different from above?
91-
assertEquals(parent.complain(), __);
92-
}
93-
94-
@Koan
95-
public void classCasting(){
96-
try{
97-
Object o = new Object(); // were downcasting way to far here - would it be possible
98-
// to even compile this koan had we done what was safe, and
99-
// held the reference as Sleepable?
100-
((Sleepable)o).sleep();
101-
}catch(ClassCastException x){
102-
fail("Object does not implement Sleepable, maybe one of the people classes do?");
103-
}
104-
}
105-
106-
@Koan
107-
public void complicatedCast() {
108-
Child parent = new Parent();
109-
// How can we access the parent's ability to "complain" - if the reference is held as a superclass?
110-
assertEquals(new Parent().complain(), __);
111-
}
112-
37+
38+
interface Sleepable {
39+
String sleep();
40+
}
41+
42+
class Grandparent implements Sleepable {
43+
public String sleep() {
44+
return "zzzz";
45+
}
46+
}
47+
48+
class Parent extends Grandparent {
49+
public String complain() {
50+
return "TPS reports don't even have a cover letter!";
51+
}
52+
}
53+
54+
class Child extends Parent {
55+
public String complain() {
56+
return "Are we there yet!!";
57+
}
58+
}
59+
60+
@Koan
61+
public void downCastWithInheritance() {
62+
Child child = new Child();
63+
Parent parentReference = child; // Why isn't there an explicit cast?
64+
assertEquals(child instanceof Child, __);
65+
assertEquals(parentReference instanceof Child, __);
66+
assertEquals(parentReference instanceof Parent, __);
67+
assertEquals(parentReference instanceof Grandparent, __);
68+
}
69+
70+
@Koan
71+
public void downCastAndPolymorphism() {
72+
Child child = new Child();
73+
Parent parentReference = child;
74+
// If the result is unexpected, consider the difference between an instance and its reference
75+
assertEquals(parentReference.complain(), __);
76+
}
77+
78+
@Koan
79+
public void upCastWithInheritance() {
80+
Grandparent child = new Child();
81+
Parent parentReference = (Parent) child; // Why do we need an explicit cast here?
82+
Child childReference = (Child) parentReference; // Or here?
83+
assertEquals(childReference instanceof Child, __);
84+
assertEquals(childReference instanceof Parent, __);
85+
assertEquals(childReference instanceof Grandparent, __);
86+
}
87+
88+
@Koan
89+
public void upCastAndPolymorphism() {
90+
Grandparent child = new Child();
91+
Parent parent = (Child) child;
92+
// Think about the result. Did you expect that? Why?
93+
// How is that different from above?
94+
assertEquals(parent.complain(), __);
95+
}
96+
97+
@Koan
98+
public void classCasting() {
99+
try {
100+
Object o = new Child(); // were downcasting way to far here - would it be possible
101+
// to even compile this koan had we done what was safe, and
102+
// held the reference as Sleepable?
103+
((Sleepable) o).sleep();
104+
} catch (ClassCastException x) {
105+
fail("Object does not implement Sleepable, maybe one of the people classes do?");
106+
}
107+
}
108+
109+
@Koan
110+
public void complicatedCast() {
111+
Grandparent parent = new Parent();
112+
// How can we access the parent's ability to "complain" - if the reference is held as a superclass?
113+
assertEquals("TPS reports don't even have a cover letter!", __);
114+
}
115+
113116
}

0 commit comments

Comments
 (0)