class GameShape{
void displayShape(){System.out.println("displaying shapes");}
}
class PlayerPiece extends GameShape implements Animatable{
void movePiece(){System.out.println("i am moving a piece");}
public void animate(){System.out.println("i am aniamtable");}
void displayShape(int i){System.out.println("method overloaded in PlayPiece");}
}
in main ,
Playerpiece p=new PlayerPiece();
GameShape g=new GameShape();
p.displayShape(23); //works
g.displayShape(34);//Not working
In
java their are two concept to be understood firstly what is required at the compile time and secondly what is needed at run time.
Now when we talking about method overloading and overridding the difference is very little.
Compiler resolves the refrence (nothing about the object) so
GameShape should be having the displayShape method with int argument.If its not having the compliation error occurs.
Now let saw i put that method in GameShape class now the complier doesn't say anything(means it compiles) but which mthod is invoked depends upon the object only: GameShape g=new GameShape();
g.displayShape(34);//
will invoke the method in GameShape class.
GameShape g=new PlayerPiece(); g.displayShape(34);
will invoke the method in the PlayerPiece class
although in this case also complier knows the method in the parent class only.