Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...