Instance (computer science)
|This article needs additional citations for verification. (June 2013)|
In object-oriented programming (OOP), an instance is a specific realization of any object. Formally "instance" is synonymous with "object", as they are each a particular value (realization), and these may be called an instance object; "instance" emphasizes the distinct identity of the object. The creation of a realized instance is called instantiation.
In class-based programming, objects are created from classes by subroutines called constructors. An object is an instance of a class, and may be called a class instance or class object; instantiation is then also known as construction. Not all classes can be instantiated – abstract classes cannot be instantiated, while classes that can be instantiated are called concrete classes. In prototype-based programming, instantiation is instead done by copying a prototype.
An object may be varied in a number of ways. Each realized variation of that object is an instance. Each time a program runs, it is an instance of that program. 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 type and your particular dog as an instance of that class.
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.
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 science 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.
Objects 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 coffee 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 class (type description), 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 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.
- "What is instance? – Definition from WhatIs.com". Whatis.techtarget.com. Retrieved February 9, 2014.