This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Oveririding confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Oveririding confusion" Watch "Oveririding confusion" New topic
Author

Oveririding confusion

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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

Joined: Jan 10, 2007
Posts: 247
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

Joined: Jul 23, 2007
Posts: 203
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.


http://subodh-gupta.blogspot.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Oveririding confusion