0

Assuming I have this two classes, in separate files:

public class Text
{
    public String _word;

    public Text(String w)
    {
        _word = w;
    }

    public String getWord()
    {
        return _word;
    }

    public boolean equals (Text other)
    {
        return ((other!=null)&&(_word.equals(other._word)));

    }
    public boolean test (Text other)
    {
        return 1==1;

    }
}

2nd class:

public class Sentence
{
    public String _word;

    public Sentence(String w)
    {
        _word = w;
    }

    public String getWord()
    {
        return _word;
    }

    public boolean equals (Object other)
    {
        return ((other!=null) && (other instanceof Sentence)
                &&  (_word.equals(((Sentence) other)._word)));
    }
}

And the following main:

public static void main(String[]args){

     Text y1 = new Text("abc");
     Sentence z1 = new Sentence ("abc");
     **
}

Let's say I run the following command where ** is:

System.out.println (y1.equals(z1));

Everything is ok, and it outputs "false".

But, if I run this command:

System.out.println (y1.test(z1));

The compiler screams "Sentence can not be converted to Text".

Two questions:

  1. Why it works for equals but not for test? y1 is Text, so calling y1.equlas() calls to equlas() inside Text, and there it gets only Text as parameter.
  2. If it DOES work, why the output is false? both "_word" set to "abc".

Thanks!

1
  • put this line in your Word.equals method System.out.println("Word equals called"); and then run the test. See what happens. Commented Feb 18, 2016 at 17:18

3 Answers 3

3

You've defined an equals(Text) method in Text. However, it doesn't override the existing equals(Object) method that it inherits from Object. Because of this, your equals(Text) method overloads the equals(Object) method in Object. Consequently, you can call y1.equals(z1). Because z1 is a Sentence, the equals(Object) method is the one called. The Sentence object matches Object but not Text. The equals method in Object compares object references to see if they're identical.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

They're not, so it returns false.

You've defined a test(Text) method in Text. There are no other overloads available, and Sentence doesn't match Text at all, so the compiler complains.

Incidentally, the equals(Object) method you've defined in Sentence is a proper override of equals, checking for null and the class of the argument.

Sign up to request clarification or add additional context in comments.

Comments

2

According to the Object class definition, you inherit this in all classes

public boolean equals(Object obj)

In your case y1.equals(z1) is actually executed as y1.equals( (Object) z1), a valid cast since all objects inherit Object. You then have the above method called.

Comments

0

I think in Text.java you wanted to override Object.equals(Object other), but instead of overriding you created an other method with the same name (equals(Text other)), but with different parameter type.

That is why System.out.println (y1.equals(z1)); compiles: that equals call matches the signature equals(Object), which method Text inherits from Object.

On the other hand, System.out.println (y1.test(z1)); fails to compile, since Text has only 1 method with the name test, and its formal parameter type is Text, which doesn't match the type of the actual parameter (Sentence).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.