• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic