From f389c94ddb8b9477741aafda6dd141e3c04cea3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 16 Feb 2017 12:14:49 +0100 Subject: java : add PatternInterfaceInheritance.java --- java/PatternInterfaceInheritance.java | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 java/PatternInterfaceInheritance.java 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 tests = new Vector(); + tests.add(new Test0()); + tests.add(new Test1()); + tests.add(new Test2()); + + for (TestInterface test : tests) test.run(); + } +} + -- cgit v1.1-2-g2b99