[ACCEPTED]-Interface Contract, Class Object?-contract

Accepted answer
Score: 37

Not really. There are four terms here, so 58 I'll go over each of them:


An interface is 57 an abstract class (in languages like Java 56 where there is no multiple inheritance, sometimes 55 there are other restrictions, such as a 54 separate data type) that is intended to 53 be used as a common base to access a number 52 of similarly-behaving objects. Conceptually, there 51 is no requirement for abstractness, but 50 usually, an interface will have at least 49 one abstract method. An interface is a method 48 for your program to communicate with a number 47 of similar classes, each with different 46 semantics but the same general purpose.


A 45 contract is the implicit agreement you make 44 between users and implementers of a class 43 or interface. For instance, preconditions 42 and postconditions (invariants are usually 41 a contract within the class' implementation 40 - generally, things like the relation between 39 internal members don't need to be exposed). The 38 specification for a return value or an argument 37 can also be part of the contract. It basically 36 represents how to use the function/class/interface, and 35 isn't generally fully representable in any 34 language (some languages, like Eiffel, allow 33 you to put explicit contracts in, but even 32 these can't always fully flesh out the requirements). When 31 you implement an interface or derive from 30 a class, you are always having to meet the 29 interface requirements, or, when overriding 28 a non-abstract class, to behave similar 27 enough that an external viewer doesn't notice 26 the difference (this is the Liskov Substitution 25 Principle; a derived object should be capable 24 of replacing the base with no difference 23 in behavior from the outside perspective).


A 22 class doesn't need a lot of going over, since 21 you clearly have used them before. A class 20 is the data type, and in some languages 19 is a superset of interfaces (which have 18 no formal definition, as in C++), and in 17 others is independent (such as in Java).


An 16 object is an instance of a class type (or 15 of any non-class type, usually). The exact 14 definition of an object is very specific 13 to a language, but the general definition 12 is the actual thing referred to by multiple 11 references/pointers to the same thing - for 10 instance, in some languages like Java, == compares 9 whether two variables are the same object, not 8 necessarily whether they are semantically 7 the same. Objects are independent from classes 6 or interfaces - they represent a single 5 instance. Another way of thinking of it 4 is that class or interface is the mold, and 3 the object is the physical object that comes 2 out of the mold (a rather bad analogy, but 1 it's the best I can come up with right now).

Score: 8

No, not really. A class is a template that 15 you define. Each object that instantiates 14 that class follows the template. They're 13 not really redundant terms, because the 12 two things are not identical. You can think 11 of a class as a user-defined data type. Classes 10 and objects are different from each other 9 in the exact same way that the primitive 8 data type int is different from the literal 7 value 3.

An interface defines a set of methods 6 that all implementing classes must support. The 5 interface itself is the contract that you 4 define for the implementing classes. It 3 just says that any class that implements 2 the interface, must have that interface's 1 set of public methods.

Score: 2

Well I guess... if an interface specifies 3 a contract than a class specifies an (or 2 multiple) instance(s) of a particular object.

Terminology 1 is less important than application though.

Score: 1

Actually, an interface is a contract, when 16 an object is an instance of a class - they 15 are different things that don't have too 14 much in common.

The interface just provides 13 a facade for objects, or a warranty for 12 the caller that the object can do some operation, even 11 without knowing it's implementation.

For 10 example, you can have two classes implementing 9 the same interface/contract, but do totaly 8 different things (even though the meaning 7 of doing them may be the same).

Take the 6 IDisposable interface for example: Each 5 object can release the resources that it 4 uses, but it can do it in many different 3 ways, it can choose not to release anything. It's 2 the object's choice.

At least this would 1 be the POV in .NET

Score: 1

To complete the previous answers, a word 16 about interfaces:

If the class is more than 15 a template for an object (because of its 14 global characteristics independent of any 13 instances), the interface can be also be 12 described as a point of view

A class implementing several 11 interface:

  • complete the contract it needs to respect
  • allow the user to see any instances of that class from the point of view of the one represented by the implemented interface.

"Point of view" means you can 10 using an object by focusing solely on the 9 contract define by that interface.

It is 8 in that aspect an interface is an "abstract 7 class", as in an "abstraction" (something 6 which takes after some of the characteristics 5 of a class, but leave some others out). In 4 java world, an interface leaves actually 3 a lot out, since it can only be applied 2 to define contract for instance, not for 1 static methods or functions.

Score: 0

"Class" and "Object" represent two different 23 things; they are related, but what they 22 represent IS different, quite strongly.

The 21 best way to describe this is to look at 20 Static. A class can have static members, which 19 are completely separate from any INSTANCE 18 of that class. Objects of that class may 17 or may not use those static members; but 16 the instance of the object of that class 15 is completely separate from any static uses 14 of that class (or should be, at the very 13 least).

Or think of the singleton pattern. Storing 12 an instance of the class object in a static 11 class accessor is a common practice, and 10 shows the difference. You refer to the 9 class static accessor to get the object instance of a singleton class; if the 8 class static member does not have an object instance to refer to, the class creates 7 the instance of the object.

Put another way; an object 6 is an instance of a class; but a class can 5 be more than just a template from which objects 4 are instantiated. Static members of classes 3 have a representation in memory that is 2 completely independent of object instances 1 of those classes.

More Related questions