Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Contract in Java

Tags:

java

scjp

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?

like image 529
Prateek Singla Avatar asked Jun 11 '13 07:06

Prateek Singla


People also ask

What is contract method in Java?

The @Contract annotation is used for defining a contract that a method must meet. This lets the IDE find problems in methods which call methods that you have annotated. You can use this annotation not only for annotating your own code but also for other existing libraries.

What is a contract in programming?

Contracts enable specifying conditions that must hold true when the flow of runtime execution reaches the contract. If a contract is not true, then the program is assumed to have entered an undefined state. Rationale: Building contract support into the language provides: a consistent look and feel for the contracts.

What is a contract class in Java?

A contract class is a public final class that contains constant definitions for the URIs, column names, MIME types, and other meta-data about the ContentProvider. It can also contain static helper methods to manipulate the URIs.

What is a contract in Object Oriented Programming?

In object-oriented programming it is natural that the program parts are classes. The preconditions and the postconditions of the public methods in a class together form a contract between the class and its clients. It can be a serious matter if a contract is broken.


1 Answers

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.

like image 200
AllTooSir Avatar answered Sep 30 '22 04:09

AllTooSir