Reputation: 89
Here's my problem : I have a class Piece that has method getName that return "P" (its just an example) I have another class King that extends Piece and has redefined the getName method to return "K".
Now I have an array of Piece (Piece[] array) (in this array there are object of instance King) When I call the getName from all these object I always get "P" from all objects. I want to get "k" from King object and "P" from Piece object. Here's an example :
Piece[] p = new Piece[1];
p[0] = new King();
p[0].getName() //I got "P" not "K"
How to resolve my problem?
Thanks.
Upvotes: 0
Views: 87
Reputation: 25008
Previous answer deleted
I have rectified the mistake in my previous answer to come up with a solution. I added a new method to the subclass called getSpecificName()
which accepts an int
argument to decide which getName()
is to be called and thus gives you the proper value.
The code is available here: http://ideone.com/ioF06I
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class IdeoneBase{
public String getName(){
return "P";
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone extends IdeoneBase
{
@Override
public String getName(){
return "K";
}
String getSpecificName(int x){
if(x == 1){
return super.getName();
}
return getName();
}
public static void main (String[] args) throws java.lang.Exception
{
IdeoneBase piece = new Ideone();
if(piece instanceof Ideone){
Ideone p = (Ideone) piece; // without this downward cast, you cannot call getSpecificName(). You can only call that is common between the two.
System.out.println(p.getSpecificName(1));
System.out.println(p.getSpecificName(999));
}
}
}
Upvotes: -2
Reputation: 56499
Since you've not showed any code, I will show you a simple example:
public class Piece {
public String getName() {
return "P";
}
}
public class King extends Piece {
@Override
public String getName() {
return "K";
}
}
public class JavaTest {
public static void showNames(Piece[] p) {
for (Piece x : p) {
System.out.println(x.getName());
}
}
public static void main(String[] args) {
Piece[] p = new Piece[]{
new Piece(),
new King()
};
showNames(p);
}
}
Output
P
K
Upvotes: 2
Reputation: 106460
Are you absolutely certain that King extends Piece
?
This is known as an is-a relationship. A King
is a Piece
, so it makes sense for it to inherit from Piece
.
public class Piece {
public String getValue() {
return "P";
}
}
public class King extends Piece {
public String getValue() {
return "K";
}
}
With this, when you instantiate a new King
, getValue()
will return "K"
, as you expect.
Upvotes: 2
Reputation: 1600
What you may want to do is make Piece
an abstract class. Since it is a base class that probably won't be instantiated directly, you will have other classes that extend it which are concrete classes, i.e. - King, Queen, Pawn, Rook, Bishop, Knight, etc.
public abstract class Piece {
... // other code
public abstract String getName();
... // other code
}
public class King extends Piece {
... // other code
@Override
public String getName() {
return "K";
}
... // other code
}
Upvotes: 2