Please can someone explain to me how the last 6 lines of the output get printed. I know that due to static binding the first three lines are printed appropriately.
I do not know why the 5th line gives the output because it is of type Ipod and it does not have any method for song but still it prints the output. Here is the code:
package myferrari;
import mycar.*;
class IPOD
{
void PlayMusic(Music m)
{
System.out.println("Music from Ipod");
}
}
class IpodPlayer extends IPOD
{
void PlayMusic(Music m)
{
System.out.println("Music from IpodPlayer");
}
void PlayMusic(Song m)
{
System.out.println("Song from IpodPlayer");
}
}
class Music{}
class Song extends Music{}
public class Main {
public static void main(String [] args)
{
IPOD ipod = new IPOD();
IpodPlayer iplayer = new IpodPlayer();
IPOD ipl = new IpodPlayer();
Music m = new Music();
Song s = new Song();
Music sm = new Song();
iplayer.PlayMusic(m);
iplayer.PlayMusic(s);
iplayer.PlayMusic(sm); // static binding refers to the reference type
ipod.PlayMusic(m);
ipod.PlayMusic(s);
ipod.PlayMusic(sm);
ipl.PlayMusic(m);
ipl.PlayMusic(s);
ipl.PlayMusic(sm);
}
}
The output is here:
Music from IpodPlayer
Song from IpodPlayer
Music from IpodPlayer
Music from Ipod
Music from Ipod
Music from Ipod
Music from IpodPlayer
Music from IpodPlayer
Music from IpodPlayer