How to invoke original method inside an overridden method - java

class Object{
String x;
int y;
Object (String a, int b){
this.x = a;
this.y = b;
#Override
boolean equals(Object obj){
return (this.x.equals(obj.x)) && this.y == obj.y);
}
}
Here I am trying to write a method that overrides equals() in order to equate two values, a string and an integer, at the same time. In order to test for string equality, I use the original equals() method that I am overriding.
Can I do this without errors? Or can I not use the original equals() method inside of a method overriding it? Is there a better way of achieving this?
I am not quite able to find answers to this question online, but that may be a result of not knowing the technical wording for a situation like this.
Thank you

I think the problem is that you're not correctly overriding the Object.equals() method. If you're trying to check that both the String and the int are equal in order for your object to be equal, it sounds like you want a new object to represent whatever the String and int together represent:
class MyObj {
private String str;
private int num;
...
}
(with appropriate getter and setter methods)
Then you can override MyObj.equals() like so:
#Override
boolean equals(MyObj that){
/* First check for null and all that stuff */
...
return this.str.equals(that.getStr()) && this.num == that.getNum();
}

Call:
super.whatevername();
Or in this case:
super.equals(someObject);
This will call the superclass' method
By the way, the original method for equals is
public boolean equals(Object obj)
By the way, your whole return block can be replaced by:
return (this.x.equals(obj.x)) && this.y == obj.y);
The way you did it is inefficient and makes me cringe :/

Related

Java equals() for Objects (Super&Subclasses)

Hi i'm just beginner learning about abstract classes & interfaces.
Everything we build our prof is testing by creating clones and comparing objects.
I've learned overriding the equals() method the detailed way…
#Override
public boolean equals(Object obj){
if (this == obj){
return true;
}
else if (obj == null){
return false;
}
...
else {
Obj x = (*Superclass*)obj;
…
}
I was now wondering if I could replace this long way by a short Version where I change the toString method, do a hashCode of the toString method and compare the hashCodes of my Objects (original & clone).
Would this be ok to do or is there any reason i shouldn't do it?
Would I be able to inherit the toString, hashCode and equals method and just adjust the clone() in subclasses if we assume that the subclasses use the same variables?
My idea was following
public abstract *Superclass*{
public String name; //would be private in org. code
public int hp; //would be private in org. code
public Superclass(){
}
#Override
public Superclass clone(){
return this; //(not sure if this is ok to use)
}
#Override
public String toString(){
Class temp = getClass();
return temp.getName() + this.name + " " + this.hp;
}
#Override
public int hashCode(){
int hcModify = 10;
int hcCurrent = this.toString().hashCode();
return hcModify * hcCurrent;
}
#Override
public boolean equals(Object obj){
return this.hashCode() == obj.hashCode())
}
}
So the first thing to note is that your equals method will throw an error if obj is null - you can't use any . operators on null.
Your clone method is dangerous if there's mutability in play - mutability means "values can be changed". Because it just returns a reference, changes will be reflected in both the original and "cloned" values (because they're the same.) This is not what most developers would expect. (I suggest looking up deep vs. shallow clones, which is related.)
x = new Thing()
y = thing.clone()
x.changeInSomeWay()
//is y now also changed?
The method of using hash codes for equality is not necessarily good or bad - it depends on the relation of the object to its hash and toString functions, and if there are colisions. Some objects will have hash or toString colisions, where different objects will have the same hash or string representations - particularly large or complex objects, where those representations don't include all of the data that you'd want to be reflected in an equality check.
Yours is actually an example of this. You're using an int hashcode, which only has 2^32 (or whatever) possible values, while Strings have, in principle, infinite possible values; by the pigeonhole principal, there must therefor be multiple objects with different names but the same hashcode.
In general, it's not a safe practice, and can lead to weird, difficult to diagnose errors.
I'm not sure why you're multiplying by 10?

Comparing objects in HashSet

I'm making a 2D game that has a stars in it. I decided to create constructor in class named Star that gives random coordinates.
public Star(){
super(0,0);
x = randomX.nextInt(maxX - minX + 1);
y = randomY.nextInt(maxX - minY + 1);
}
Then, in other class I put them in HashSet
Set<Star> star = new HashSet<>();
public Set<Star> generateStars(){
while (star.size() < numberOfStars){
star.add(new Star());
}
return star;
}
Of course, I have render and tick methods but I think it's not worth to paste them. My lecturer told me that there can be same stars and to prevent that I should use identity function using hashcodes. Can someone help me figure that out ? I imagine that this function should check if the hashcodes are the same and if it's the case it should return only one value that way we will add 1 object instead of 2 into the HashSet. Am I right ?
Overriding the hashCode() method alone in your Star class will not work you will have to override the equals() method.
See the following code where we don't override the equals() method:
class Star {
int x, y;
public Star(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public int hashCode() {
return Objects.hash(x, y);
}
}
public class Main {
public static void main(String[] args) {
Star s1 = new Star(0, 0);
Star s3 = new Star(0, 0);
Star s2 = new Star(31, -31*31);
Set<Star> set = new HashSet<>();
set.add(s1);
set.add(s2);
System.out.println(set.size());
}
}
This will print 3 (instead of 2 what you might expect).
The reason for this is that the add method of java.util.Set compares 2 objects based on equals() method and not based on hashCode() method.
In the above code for the Star class, if you add the equals() method the output will be 2 now. For your reference the you can override the equals() method as follows:
#Override
public boolean equals(Object startObject) {
if (this == startObject) return true;
if (startObject == null || getClass() != startObject.getClass()) return false;
Star star = (Star) startObject;
return x == star.x &&
y == star.y;
}
So why do you need to add hashCode()?
As you are using HashSet the add method behind the scene will call the equals() method and will also call hashCode() to decide the bucket in which the new object should be put. To maintain the contract of hashCode() and equals() Both should be overridden.
When ever you override equals(), it is recommended to override hashCode() also. (Vice-versa is also true). See this link for details.
Contract for hashCode() and equals(): If for two objects say o1 and o2, o1.equals(o2) is true then hash of o1 and o2 should be same.
Make sure that you understand this properly, from the above statement it is not implied that if the hash of 2 objects are same, then o1.equals(o2) should return true. It can be possible that for 2 objects, their hash is same but the o1.equals(o2) returns false
See here, what Object's hashCode() method guarantees.
See this link to get more detailed information about this topic.
In java, when adding an object to a HashSet, the add method uses the 'equals' method, which is part of the Object class (however you can override it) in order to determine if the set already contains the object you are trying to add, see : https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
However, if you are overriding the equals method then you should also override the hashCode method, this is very well explained in the following post : Why do I need to override the equals and hashCode methods in Java?
If you are going to follow this advice, I would also advise using ApacheCommonsLang EqualsBuilder and HashCodeBuilder if your lecturer allows it as they provide a solid implementation based on the rules laid out in the book 'Effective Java' by Joshua Bloch
To override the equals method in your Star class, you want to think about the criteria that make two star objects equal. From your example this might be that both of them have the same x and y co-ordinates.
you can override hashCode() method from Object. So, in your Star class, add:
#Override
public int hashCode() {
in hash = .... //make your hash code about the coordinates of your star.
return hash;
}
so, when you place a new star with the same co-ordiante in the hash map, it will overwrite the previous start with the same co-ordiantes if already present in the map.
You have to implement hashCode and equals.
the code should be like this:
public static class Star {
int x, y;
public Star(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public int hashCode() {
return x * 1000 + y;
}
#Override
public boolean equals(Object obj) {
if (obj instanceof Star) {
Star s = (Star) obj;
return this.x == s.x && this.y == s.y;
}
return false;
}
}
public static void main(String args[]) throws Exception {
HashSet<Star> set = new HashSet<Star>();
set.add(new Star(1, 1));
set.add(new Star(1, 1));
System.out.println(set.size());
}
note: choose the suitable hashcode function for you. I assumed here that y should always be less than 1000.
If you are using eclipse as your editor just right-click on the editor pane, go-to source then generate hashcode() and equals() pick the parameters you would like to consider. You will get an autogenerated function.

Two objects with same data are not the same?

I have a test class like so:
public class CompareObjects {
public static class Obj {
public int i;
public Obj(int i) {
this.i = i;
}
}
public static void main(String[] args) {
Obj o1 = new Obj(0);
Obj o2 = new Obj(0);
if(o1 == o2) {
System.out.println("Equal");
}else{
System.out.println("Not equal");
}
}
}
I though the test would return "Equal", but it didn't. Why doesn't Java consider two objects with equal components not the same? Am I doing something wrong here? I have a feeling I completely overlooked something when I started learning Java.
Also, I tested the two against each other with the equals() method, and I get the same result. My reason for this question is that I would like to be able to test an ArrayList with the contains() method to see if one object has the same components as another and therefore equal. Any ideas?
== compares the references to the object. For example:
Obj a = new Obj(0);
Obj b = a;
//a == b
Try implementing equals():
public static class Obj {
public int i;
public Obj(int i) {
this.i = i;
}
#Override
public boolean equals(Object other) {
if (other == this) return true;
if (!(other instanceof Obj) || other == null) return false;
return i == ((Obj)other).i;
}
#Override
public int hashCode() {
return i;
}
}
Then, you can use if(o1.equals(o2)) {. However, this is not really a good example, read this (link) for more information.
== returns true only if you are comparing the same object [i.e. the same memory location].
If you want to compare objects by their fields, you have to overload the equals() method, in order to induce an equivalence relation over them.
public boolean equals(Object other){
return this.i == other.i;
}
Be sure that the equals() method respects reflexivity, symmmetry, transitivity.
== compares the reference equality, i.e if they refer to the same object in the memory.
You need to override equals() method, and use it whenever you want to compare their values. Also, override hashCode() which is used by HashMap for example.
The == operator does not check for equality in class data; rather, it checks to see if they are the same location in memory. If you did o2 = o1 instead of initializing them the same way, they would be the same location in memory, so o2==o1 would return true. However, since they were initialized some separately, it returns false. Instead, you should define an equals method and implement that.

Which function should I override when using indexOf() function

Which function should I override when using the indexOf() function in java. I have a array list, then I take in an input as the ID and create a object which contains the ID and all the other elements are null, then I need to pass that object and get the index of the element which contains that object
The equals() method
public boolean equals(Object o) {
if (o instanceof MyObject) {
//id comparison
MyObject mo = (MyObject)o;
return mo.id.equals(id);
}
return false;
}
Change MyObject to your class.
Remember to change hashCode() as well as #Hovercraft points out. equals and hashCode go together (read the javadoc for them). Else you might run into some nasty and possibly hard to find bugs.
An example:
With java 7+ you can do this:
public int hashCode() {
return java.util.Objects.hashCode(id);
}

setting hashcode and equals to create a set with unique object

I would create a Set exactly HashSet to contains only char.for example a,b,c,d,e,f,g...
but these chars are not represented by the primitive type but I have an object
public FirstChar{
private char c;
public FirstChar(char c){
this.c = c;
}
}
Now i want to add the object FirstChar into a set but to avoid repeated elements I have to implement HashCode() and equals()
I know how to implement equals but how can i implement hashcode in the way I could have only one element in the set?
NB. Please don't say me to use Eclipse
EDIT: I've just read your comment that you only want one letter in the entire set - which sounds like a very odd requirement, but it's basically fulfilled by something like:
public final class FirstChar {
private final char c;
public FirstChar(char c) {
this.c = c;
}
#Override public int hashCode() {
return 0;
}
#Override public boolean equals(Object other) {
return other instanceof FirstChar;
}
}
In other words, every instance of FirstChar is deemed equal to every other instance, and they all have the same hash code. As I say, this is really strange... is it definitely what you want?
Original answer
Implementing hashCode() for a value which only logically has a single character is easy:
#Override
public int hashCode() {
return c; // Use implicit conversion to int
}
Check against the contract of Object.hashCode and you'll find this works fine - assuming your equals method basically just compares values of c. (It's not clear what you meant when you wrote "in the way I could have only one element in the set" - I assume you mean only one element per distinct character.)
However, I'm confused as to what value your FirstChar class provides over just using java.lang.Character. Is there any reason you can't just use a Set<Character>?
In any case, from what I can tell, the hashCode method in the Character class simply returns the char as an int.
In your specific case, if you want the set to only contain the first FirstChar added to it, you can make all FirstChars equal to each other:
class FirstChar{
private char c;
public FirstChar(char c){
this.c=c;
}
public String toString(){
return String.valueOf(c);
}
public boolean equals(Object o){
return o instanceof FirstChar;
}
public int hashCode(){
return 42;
}
}
But unless you have a very good reason, this doesn't sound like a good idea.

Categories