Matching a method signature and binding a method implementation are two sepearate issue so ,what's wrong with the follows code?
Container c5 = new JButton();
Object c6 = new JButton();
c5.add(c6); //----it is wrong,why?
for me , c5 is a reference variable which contain reference to object of JButton,and JButton extends the class Component,so it should be right so why ?
Java is a Single-Dispatch language. That means its method signature analysis is done at compile time, not run time.
The type of
c6
isObject
. (Yes, you assign aJButton
, but the declared type of the variable is stillObject
.) So when Java looks atc5
(aContainer
) and its methods, it doesn't see anadd()
method that takes an Object. So it flags it as an error.