Leem.fin
Leem.fin

Reputation: 42622

What does "contract" of a class mean

I am reading the book The Java Programming Language. In the chapter which explains overriding method, it says:

Making an override method (in subclass) less accessible than it was in super class would violate the contract of the superclass ...

The text as a whole is understandable. My only question is what is contract of the superclass? What does the contract mean for a Java class?

Upvotes: 41

Views: 47404

Answers (8)

Victor Sorokin
Victor Sorokin

Reputation: 12006

Contract of type (class, interface, enum) is the, well, the contract this type promises to comply to. It states:

  • what are acceptable parameters to constructor and/or methods of this type;
  • what you should and/or should not expect this type to be doing. For example, it may state that you shall expect this class to be thread-safe or not thread-safe;
  • what are invariants supported by this type. For example, type method addData(float) of MathAverage class which calculates average of its input may state that every time that your call to add(float) returns, you shall expect call to MathAverage.getAverage() to return correct average of current input.
  • generally speaking, your type can specify any constraint all it's subtypes must follow. For example, it may say "no method of this type must take longer than 1 second to execute".

Contract is specified in free-form in javadoc of type. There are some tools/practices to enforce execution of contracts, but they are limited, exactly because contract can be arbitrary, or, even, self-contradictory, in case of programmer's error.

Since subtyping(subclassing) can extend/modify behavior of supertype methods in arbitrary way, it may, as well, violate some parts of supertype's contract. Example of this would be extending HashMap, which accepts null values and keys, with some implementation which prohibits null values in calls to it's methods.

Other important aspect about type contract is that subtype can have stronger contract (covering subset of constraints in type's contract), but can't have weaker contract (covering superset of constraints in type's contract). For example, if your type's method 'doX(n)' promises to take O(n) (linear) time, 'doX(n)' in subtype can take O(1) (constant) time, but can not take O(n^2) time.

Upvotes: 8

David
David

Reputation: 73574

A contract in in a Java class is similar to a contract in the real world - In non-technical terms:

It's an agreement that the class will expose certain methods, certain properties, and certain behaviors.

More technical, from here: (bold added by me)

Wouldn't it be nice if all Java classes that you use, including your own, lived up to their promises? In fact, wouldn't it be nice if you actually knew exactly what a given class promises? If you agree, read on [...]

Design by Contract

The Design by Contract (DBC) software development technique ensures high-quality software by guaranteeing that every component of a system lives up to its expectations. As a developer using DBC, you specify component contracts as part of the component's interface. The contract specifies what that component expects of clients and what clients can expect of it.

Upvotes: 42

dgvid
dgvid

Reputation: 26633

The contract of a class or interface, in Java or any other OO language, generally refers to the publicly exposed methods (or functions) and properties (or fields or attributes) of that class interface along with any comments or documentation that apply to those public methods and properties.

In the case of the relationship between a class and subclass, any protected methods or properties would be considered "publicly exposed," in the sense that they are exposed to the subclass.

Upvotes: 1

Pops
Pops

Reputation: 30828

It's an expression that comes from the idea of contracts in the "real world."

Basically, if you break your contract with a class, then it's not required to behave the way you might expect. From the other direction, it's a promise by the class that if you follow the rules it sets out, it will behave the way its API says.

One common example of a contract in Java is overriding equals() when hashCode() is overridden. Two objects that are considered equal must, by contract, have the same hash code. It's possible to write code that's syntactically correct that doesn't obey this, but it might not work properly, and that's the fault of the programmer who broke the contract.

Upvotes: 0

sum2000
sum2000

Reputation: 1403

in simple terms it means you would break the conditions of superclass if u make override code less accesible

Upvotes: 0

Mansuro
Mansuro

Reputation: 4617

There is many principles that you should conform to when programming in java, or in any programming languages. The principles depend on the programming language you are using. You can know more about contract in Design by contract wikipedia page

Upvotes: 0

cdeszaq
cdeszaq

Reputation: 31280

A class's "Contract" is it's public interface or at least the interface if presents to classes other than itself.

This means that it includes any elements (methods, fields, constructors, etc.) that other classes can use.

Upvotes: 3

jontro
jontro

Reputation: 10628

It means that method overriding a method on a parent class or interface must behave in the way that the contract defines.

Otherwise the result is undefined.

Upvotes: 3

Related Questions