add test polymorphism example

This commit is contained in:
Oleksii Aleshchenko 2023-04-27 16:08:09 +03:00
parent 484bef389b
commit e41e64369b
4 changed files with 32 additions and 0 deletions

7
src/test/one/A.java Normal file
View File

@ -0,0 +1,7 @@
package test.one;
public class A implements MyMethodInterface {
public void myMethod() {
}
}

7
src/test/one/B.java Normal file
View File

@ -0,0 +1,7 @@
package test.one;
public class B implements MyMethodInterface {
public void myMethod() {
}
}

13
src/test/one/Main.java Normal file
View File

@ -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();
}
}
}

View File

@ -0,0 +1,5 @@
package test.one;
public interface MyMethodInterface {
void myMethod();
}