abstract class
public abstract class Animal {
private int id;
private String name;
public Animal(int id, String name) {
this.id = id;
this.name = name;
}}
_child of animal 1
public class Tiger extends Animal implements Dangerous {
public Tiger(int id, String name) {
super(id, name);
} }
_child of animal 2
public class Panda extends Animal implements Harmless{
public Panda(int id, String name){
super(id, name);
}}
_ Two attribute interfaces
public interface Dangerous {}
public interface Harmless {}
public class Zoo {
public static <T extends Animal & Harmless> void tagHarmless(Animal animal) {
System.out.println("this animal is harmless");
}
public static <T extends Animal & Dangerous> void tagDangerous(Animal animal) {
System.out.println("this animal is dangerous");
}}
public class App {
public static void main(String[] args) {
Animal panda = new Panda(8, "Barney");
Animal tiger = new Tiger(12, "Roger");
Zoo.tagHarmless(panda);
Zoo.tagHarmless(tiger);
}}
-result
this animal is harmless
this animal is harmless
Process finished with exit code 0
i try to restrict the methods of the class "zoo" with the interfaces "Dangerous" and "Harmless".
with the code
public static <T extends Animal & Harmless> void tagHarmless(Animal animal).
The Tiger doesnt have this Interface, so it actually should not work, does it? But the tiger can also be added into this method tagHarmless.
I don't see the mistake.
Thanks for help.