• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java subclass question

cchen

Diamond Member
Say I have a class Player, it has a method getRounds that takes some parameters

now say I have two subclasses of Player called Human and Computer, they both have methods getRounds that don't take any parameters
now I have a game class, in which I have two instances of players, in the constructor I cast both of the players, one to Computer and one to Human
in the game class, I have a method play, where I call player1.getRounds() and player2.getRounds(). the compiler is telling me that the playRounds() is not found in class Player...

I'm confused because I cast the Players already... so shouldn't it be calling the getRounds in Human and computer?

so I got the answer thanks to a helpful member on the board saying that i should cast the player as i call the getRounds, like ((Human)player1).getRounds.... it compiles, but now i get an exception java classcastexception exception... any ideas how to fix this?
 
It doesn't work because getRounds() taking no parameters is not defined in class Player (as the compiler states). So you simply can't polymorphically call that method on a Player reference (even if the actual object instance is a Human or Computer). Casting it differently won't help; a ClassCastException is a runtime exception so you're just deferring the problem from compilation to runtime.

FWIW, with the exception of collections (either the standard library or your own design), the presence of casting is usually an indication of a design flaw.

While there is a workaround (that I won't mention), I don't see why class Player doesn't just declare getRounds() that takes no parameters. If it did, subclasses could override that method, and the polymorphic call would work as you originally expected.
 
Ok, so I changed the getRounds in player to take no parameters...

now how do I get getRounds to be overridden ?
 
Say you have ...

public class Player{
....public void getRounds(){}
}

public class Human extends Player{
....public void getRounds(){}
}

public class Computer extends Player{
....public void getRounds(){}
}

If your Human and Computer objects are cast as Player objects, the subclass methods will automatically be detected and called:

public class Test{
....public void f1(Player p1, Player p2){
........p1.getRounds(); // calls Human.getRounds()
........p2.getRounds(); // calls Computer.getRounds()
....}

....public static void main(String[] args){
........Human h1 = new Human();
........Computer c1 = new Computer();
........f1(h1, c1);
....}
}
 
If your Human and Computer classes will be overriding each of the methods in Player, you can make Player an abstract class. This will prevent you from instantiating a Player object and will allow only subclass method implementations.
 
Back
Top