This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Oveririding confusion  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused in Overriding methods concept.

Here is an example from K & B book ,chaptter2.

Method call is decided run time ,based on object type but which method,method selection is based on declred reference type.

so according to this .

<code>
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

Why this statement doesnt work ,when method call is executed at run time & actual object method is called.

Please help me clearing this simple concept.I am not clear with K &B
thank you.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g is a GameShape and not a PlayerPiece.

In GameShape displayPiece() does not take a parameter. Hence when you call g.displayPiece(34) it has no method to call. If you said g.displayPiece() all would be fine.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!