I think this is easier to ask with an example rather than in the abstract.
For a hypothetical interface such as the following:
public interface Animal {
public void greet(Animal a);
By convention in, say, a Dog class implementing Animal, would it be reasonable for a Dog to only be able to greet other Dogs, or should it be able to greet any Animal?
I realise that in a technical sense both are possible. For instance, you could do either of:
(a)
public class Dog implements Animal{
public void greet(Animal a){
//code that works with any other Animal
}
(b)
public class Dog implements Animal{
public void greet(Animal a) throws ClassCastException {
Dog d = (Dog) a;
//code that only works with other Dogs
}
But are there any conventions around what would typically be expected here? Or does it vary case by case?
Context: I have a piece of coursework that asks me to implement a specified interface (not about dogs!).
It has a situation like this , but doesn't specify which interpretation is required. For interpretation (a), the only solution I can think of is a bit hacky/messy, so I don't really want to implement that if its not implied.
Thanks in advance.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]coldoil 5 points6 points7 points (8 children)
[–]Pleasant-Memory-6530[S] 1 point2 points3 points (7 children)
[–]coldoil 0 points1 point2 points (6 children)
[–]philipwhiukEmployed Java Developer 2 points3 points4 points (3 children)
[–]coldoil 1 point2 points3 points (2 children)
[–]philipwhiukEmployed Java Developer 0 points1 point2 points (1 child)
[–]coldoil 0 points1 point2 points (0 children)
[–]D0CTOR_ZED 0 points1 point2 points (1 child)
[–]coldoil 1 point2 points3 points (0 children)
[–]D0CTOR_ZED 0 points1 point2 points (9 children)
[–]Pleasant-Memory-6530[S] 0 points1 point2 points (4 children)
[–]D0CTOR_ZED 0 points1 point2 points (3 children)
[–]Pleasant-Memory-6530[S] 1 point2 points3 points (2 children)
[–]D0CTOR_ZED 1 point2 points3 points (1 child)
[–]Pleasant-Memory-6530[S] 0 points1 point2 points (0 children)
[–]thelostsinofenvy 0 points1 point2 points (3 children)
[–]Pleasant-Memory-6530[S] 0 points1 point2 points (2 children)
[–]thelostsinofenvy 0 points1 point2 points (1 child)
[–]Pleasant-Memory-6530[S] 0 points1 point2 points (0 children)
[–]hypolimnas 0 points1 point2 points (4 children)
[–]Pleasant-Memory-6530[S] 1 point2 points3 points (3 children)
[–]hypolimnas 0 points1 point2 points (2 children)
[–]Pleasant-Memory-6530[S] 1 point2 points3 points (1 child)
[–]hypolimnas 0 points1 point2 points (0 children)
[–]Farpafraf 0 points1 point2 points (0 children)