What is a Contract in Java
Asked Answered
T

3

5

I was solving some questions for my OCA prepration. I found this problem at Oracle's website listing sample questions for exam.

Code:

public class MyStuff {
    MyStuff(String n) { name = n; }
    String name;
    public static void main(String[] args) {
        MyStuff m1 = new MyStuff("guitar");
        MyStuff m2 = new MyStuff("tv");
        System.out.println(m2.equals(m1));
    }
    public boolean equals(Object o) {
        MyStuff m = (MyStuff)o;
        if(m.name != null) return true;
        return false;
    }
}

Question:

What is the result?

  1. The output is "true" and MyStuff fulfills the Object.equals() contract.
  2. The output is "false" and MyStuff fulfills the Object.equals() contract.
  3. The output is "true" and MyStuff does NOT fulfill the Object.equals() contract.
  4. The output is "false" and MyStuff does NOT fulfill the Object.equals() contract.
  5. Compilation fails.
  6. An exception is thrown at run time.

Answer is-

3. The output is "true" and MyStuff does NOT fulfill the Object.equals() contract.

I understand how, and why the output is true, but what I am not getting is that How come it does not fullfill the Object.equals() contract, and what exactly is a "Contract" in Java, and what if we don't abide by it?

Truce answered 11/6, 2013 at 7:45 Comment(0)
S
3

The contract here has the english meaning as well as the convention of "design by contract". The contract in terms of equals() and hashcode() method is found on the javadocs of Object - I am assuming you have read it.

The wikipedia page is a must read for better understanding of "design by contract" paradigm. In Java, interfaces are typically defined to establish a contract which the implementation classes then have to abide by.

Sidero answered 11/6, 2013 at 7:48 Comment(3)
What if we don't design by contract?Truce
@PrateekSingla Did you read the wiki page - in your example, say in java when you don't obey the contract exposed by equals() and hashcode() method from Object class, you will run into code bugs as your code will behave non deterministically (think of putting this object into a set and you might not be able to retrieve it with a different "equal" reference).Sidero
It doesn't matter whether you use "design by contract" or not: if you don't adhere to the contracts in code that you write or use then you are likely to have serious bugs / incorrect code.Bechance
P
8

The equals method implements an equivalence relation on non-null object references:

  1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  2. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false.

The below method doesn't fulfill this obligation For any non-null reference value x, x.equals(null) should return false.

public boolean equals(Object o) {
    MyStuff m = (MyStuff)o;
    if(m.name != null) return true;
    return false;
}

This will throw a ClassCastException , if o is null , it should ideally return false.

Piecedyed answered 11/6, 2013 at 7:49 Comment(2)
Still this code is bad because the equals() method should not throw a ClassCastException if another object is supplied to the method.Abrahan
If o is null it will throw a NPE on dereference of name. If o is non-null and not of type MyStuff it will throw a CCE. Both are not according to contract.Torrence
S
3

The contract here has the english meaning as well as the convention of "design by contract". The contract in terms of equals() and hashcode() method is found on the javadocs of Object - I am assuming you have read it.

The wikipedia page is a must read for better understanding of "design by contract" paradigm. In Java, interfaces are typically defined to establish a contract which the implementation classes then have to abide by.

Sidero answered 11/6, 2013 at 7:48 Comment(3)
What if we don't design by contract?Truce
@PrateekSingla Did you read the wiki page - in your example, say in java when you don't obey the contract exposed by equals() and hashcode() method from Object class, you will run into code bugs as your code will behave non deterministically (think of putting this object into a set and you might not be able to retrieve it with a different "equal" reference).Sidero
It doesn't matter whether you use "design by contract" or not: if you don't adhere to the contracts in code that you write or use then you are likely to have serious bugs / incorrect code.Bechance
L
0

User scorpion answered what a contract is.

if you don't full fill the contract, them your code might behave unexptected e.g when you use java Collections. I remember one of my collegues said: This java Collection has a bug, it does not work. But the bug was his equals method, not working togehther with hashcode()

Lariat answered 11/6, 2013 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.