In java, there are 3 behaviors related to your topic:
- overriding (official documentation here )
- overloading (official documentation here - section "Overloading Methods")
- hiding (official documentation here )
Since the documentation states that:
This is a list of the parameters of the parameters. An overriding method can be returned. This subtype is a covariant return type.
and in your case you have violated the rule number and type of parameters , it turns out that this is not override. At the same time, the following is written regarding overload:
The Java programming language supports the different methods of Java. If you have different parameter lists, you can find out what you’re talking about.
What exactly relates to your situation. Thus, calling the method by the Pair p link you will call the Pair class method, despite the fact that this is actually a Detail object.
In addition, java has a mechanism for tracking such misunderstandings in the form of an @Override annotation, marking the compiler method with it will give an error if this is not override. In your case:
@Override public void getObject(Date o){ System.out.println("Text from Detail");}
The compiler will generate the error "Method doesn't override method from its superclass" .
Well, in the end, in order to achieve true overrid in the class Detail method should look like this:
public void getObject(Object o){ System.out.println("Text from Detail");}
In this case, you will get the expected result.
I hope I managed to explain everything in detail. Good luck further study =).