summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2017-02-16 12:14:49 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2017-02-16 12:14:49 +0100
commitf389c94ddb8b9477741aafda6dd141e3c04cea3d (patch)
tree5022bce1a4d562632c94ede84439dccc16a398ab
parent754da8a2b728f513a03fab38e41688c497675898 (diff)
downloadshare-f389c94ddb8b9477741aafda6dd141e3c04cea3d.zip
share-f389c94ddb8b9477741aafda6dd141e3c04cea3d.tar.gz
java : add PatternInterfaceInheritance.java
-rw-r--r--java/PatternInterfaceInheritance.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/java/PatternInterfaceInheritance.java b/java/PatternInterfaceInheritance.java
new file mode 100644
index 0000000..a4897df
--- /dev/null
+++ b/java/PatternInterfaceInheritance.java
@@ -0,0 +1,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();
+ }
+}
+