I want to write a method that gets an object of the same type as argument, but to declare it in the base class, but I don’t want it to accept other object that derived from same base.
For example, I have the class Animal, and I want to write a method sonOf(Animal a), but I don’t want someone to do something like that:
Animal bolt = new Dog();
Animal mittens = new Cat();
bolt.sonOf(mittens);
I think a generics is needed, but I’m not sure how to use it correctly.
Thank You.
Hm. You could try, for example:
Then you get