From e41e64369b1ce4572583189f546e635c06199142 Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 27 Apr 2023 16:08:09 +0300 Subject: [PATCH] add test polymorphism example --- src/test/one/A.java | 7 +++++++ src/test/one/B.java | 7 +++++++ src/test/one/Main.java | 13 +++++++++++++ src/test/one/MyMethodInterface.java | 5 +++++ 4 files changed, 32 insertions(+) create mode 100644 src/test/one/A.java create mode 100644 src/test/one/B.java create mode 100644 src/test/one/Main.java create mode 100644 src/test/one/MyMethodInterface.java diff --git a/src/test/one/A.java b/src/test/one/A.java new file mode 100644 index 0000000..323abbd --- /dev/null +++ b/src/test/one/A.java @@ -0,0 +1,7 @@ +package test.one; + +public class A implements MyMethodInterface { + public void myMethod() { + + } +} diff --git a/src/test/one/B.java b/src/test/one/B.java new file mode 100644 index 0000000..7e049c4 --- /dev/null +++ b/src/test/one/B.java @@ -0,0 +1,7 @@ +package test.one; + +public class B implements MyMethodInterface { + public void myMethod() { + + } +} diff --git a/src/test/one/Main.java b/src/test/one/Main.java new file mode 100644 index 0000000..7e620f8 --- /dev/null +++ b/src/test/one/Main.java @@ -0,0 +1,13 @@ +package test.one; + +public class Main { + public static void main(String[] args) { + Object[] objects = { + new A(), + new B(), + }; + for (Object object : objects) { + ((MyMethodInterface)object).myMethod(); + } + } +} diff --git a/src/test/one/MyMethodInterface.java b/src/test/one/MyMethodInterface.java new file mode 100644 index 0000000..282ee3c --- /dev/null +++ b/src/test/one/MyMethodInterface.java @@ -0,0 +1,5 @@ +package test.one; + +public interface MyMethodInterface { + void myMethod(); +}