• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Oveririding confusion

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic