Instance (computer science)
| This article does not cite any references or sources. (August 2012) |
|
|
This article needs more links to other articles to help integrate it into the encyclopedia. (November 2012) |
An instance, in object-oriented programming (OOP), is a specific realization of any object. An object may be varied in a number of ways. Each realized variation of that object is an instance. The creation of a realized instance is called instantiation.
Each time a program runs, it is an instance of that program. In languages that create objects from classes, an object is an instantiation of a class. That is, it is a member of a given class that has specified values rather than variables. In a non-programming context, you could think of "dog" as a class and your particular dog as an instance of that class.[1]
Comprehensive Explanation to Newcomers [edit]
In computer science the term class has a synonym: type.
The meaning of the term type in computer science is similar to the meaning of the word "type" in everyday language. For example, a barman can ask a client, what type of beverage does he/she want? Coffee, tea, beer? A particular cup of coffee that the client receives is in the role of an instance. Two cups of coffee would form a set of two instances of (the type) coffee.
In computer science the term instance has a synonym: object.
Just like in everyday life, in computer science the types (classes) can be combined and derived from eachother. For example, the Cappuccino is assembled from milk and coffee.
Sometimes types can be seen as if they formed hierarchies. For example, the Cappuccino and Latte can be viewed as if they were derived from the type "coffee". In computer siceince lingua: "Coffee is a generalization of the "Cappuccino" and "Latte". A classical Turing tarpit of novice programmers is that in real life a single type can be derived from multiple types. For example, "Cappuccino" is derived from "milk" by adding coffee as an ingredient. This entails that instead of forming "upside-down trees" the type hierarchies form a more complex, ordered-graph like structure.
One of the central ideas of the object-oriented programming is that instead of describing new types from scratch, programmers can describe ("create", "define") new types by describing the difference between the new type and some existing, already described, type. For example, to describe the content of the [Cappuccino], the programmer only needs to write that the Cappuccino consists of coffee and milk and there is no need for writing that the coffee consists of water and roasted coffee beans and that the milk consists of proteins and water.
Types ("classes" in Computer Science lingua) consist of two types of ingredients: data-fields (usually called as "attributes" or "fields", but sometimes also called as "records") and code blocks (usually called as "methods", but sometimes called as "procedures" or "functions"). For example, the type "coffee" might contain a data-field that describes the amount of coffe left in a particular "coffee" (cup) instance. Historically the code blocks were meant to be used for manipulating mostly the data-fields of the instance that contained the code blocks in its type description (class), but in practice the code blocks are used in any way the programmers see fit. The programming techniques, design ideas, are called design patterns.
The notion of types (classes) and instances is not strictly related to programming languages and is a design idea, "design pattern", in itself. For instance, a set of files in a folder can be seen as a set of data fields of one, particular, instance. Before the emergence of object oriented programming languages the classes and instances were only design pattern terms.