blob: a4897df8a8e339d36099cd4495fe95d5913c57dd (
plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
import java.util.List; // interface
import java.util.Vector; // implementation of interface List
interface TestInterface {
public void run();
}
class Test0 implements TestInterface {
// simple inheritance and method overriding -> polymorphism
class Thing {
public void sayHello() { System.out.println(" class Thing says I'm a "+this.getClass().getName()); }
}
class Chicken extends Thing {
@Override
public void sayHello() { System.out.println(" class Chicken says I'm a "+this.getClass().getName()); }
}
class Egg extends Thing {
@Override
public void sayHello() { System.out.println(" class Egg says I'm a "+this.getClass().getName()); }
}
public void talk(Thing t) { t.sayHello(); }
@Override
public void run() {
System.out.println(this.getClass().getName());
talk(new Thing());
talk(new Chicken());
talk(new Egg());
}
}
class Test1 implements TestInterface {
// simple inheritance and method overriding -> polymorphism
// forcing method override with abstract class (can't be instanciated) and method
abstract class Thing {
// no default behavior
public abstract void sayHello();
// usage of abstract method that can't be modified
public final void talk() { sayHello(); }
}
class Chicken extends Thing {
@Override
public void sayHello() { System.out.println(" class Chicken says I'm a "+this.getClass().getName()); }
}
class Egg extends Thing {
@Override
public void sayHello() { System.out.println(" class Egg says I'm a "+this.getClass().getName()); }
}
@Override
public void run() {
System.out.println(this.getClass().getName());
(new Chicken()).talk();
(new Egg()).talk();
}
}
class Test2 implements TestInterface {
// wrong usage of an interface, this should be used for non related classes that would implement something in common
// ie: Interface Comparable, which specify the behavior of a particular data type
// or to take advantage of multiple inheritance of types.
interface Thing {
public void sayHello();
}
class Chicken implements Thing {
@Override
public void sayHello() { System.out.println(" class Chicken says I'm a "+this.getClass().getName()); }
}
class Egg implements Thing {
@Override
public void sayHello() { System.out.println(" class Egg says I'm a "+this.getClass().getName()); }
}
public void talk(Thing t) { t.sayHello(); }
@Override
public void run() {
System.out.println(this.getClass().getName());
talk(new Chicken());
talk(new Egg());
}
}
public class PatternInterfaceInheritance
{
public static void main (String [] args )
{
List<TestInterface> tests = new Vector<TestInterface>();
tests.add(new Test0());
tests.add(new Test1());
tests.add(new Test2());
for (TestInterface test : tests) test.run();
}
}
|