Say you have the following classes
public abstract class Symbol {
private char symbol;
......... //constructor
@Override
public String toString() {
return new Character(symbol).toString();
}
}
public class Nonterminal extends Symbol {
........ //constructors
@Override
public String toString() {
return super.toString();
}
}
public class Terminal extends Symbol {
..... //constructors
@Override
public String toString() {
return super.toString();
}
}
Will this compile or not, will it print out "it works" or not and why, and how would you make it work?
......
Nonterminal nt = new Nonterminal("S");
Terminal st = new Terminal("S");
if(nt.equals(st)){
System.out.println("It works");
}
.....
Not a particularly hard question, but oh well .... my java knowledge is fairly limited.
No one here has been trying to answer it by now, so I will to keep this series going.
It will compile (assumed that the code parts not given are correct).
It won't print "it work's". The reason is the default implementation of equals:
public boolean equals(Object obj) {
return (this == obj);
}
The == will only be true if the memory addresses of the objects are the same. In this case they aren't.
In order to make it work you would have to override equals in Symbol.
Example implementation:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Symbol) {
Symbol other = (Symbol) obj;
if (symbol != other.symbol)
return false;
}
return true;
}
//if you change equals you have to change hashCode too
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + symbol;
return result;
}
But: It is a bad idea to make objects with two different subtypes equal to each other. They wouldn't need different types if they where equal.