Difference between revisions of "Instance"

esse quam videri
Jump to: navigation, search
(Definition)
(Explanation)
 
(3 intermediate revisions by one other user not shown)
Line 12: Line 12:
  
 
=Explanation=
 
=Explanation=
"Instance" emphasizes the distinct identity of the object.
+
"Instance" emphasizes the distinct identity of the object, and instantiation is one of the core elements that make up object-oriented programming! Think of it this way - instances are the objects to which we orient our programming. Makes a lot more sense that way, huh?
 +
 
 +
Creating instances allows us to determine the behaviors of an object of the class they're created from. When you call an object in code, you can tell it to invoke one of its data variables or functions!
 +
From a programming point of view, an instance object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed to be part of a class's hierarchy.
  
 
Think of a coffee shop - there are a lot of different ''types'' of coffee shops around, but they each have similar characteristics.
 
Think of a coffee shop - there are a lot of different ''types'' of coffee shops around, but they each have similar characteristics.
There are different flavors, sizes, prices, and styles - but they're all still making coffee!
+
So, let's make a COffeeShop coffee class, and create an INSTANCE [[Object|object]] of that class:
 
 
Let's create a [[Class|class]] called Coffee with the properties flavor, size, and price  (It is highly recommended that you take a look at [[Programming Tutorial: Classes Part 1]] to understand the specifics of what's written below!):
 
 
 
<syntaxhighlight lang="csharp">class CoffeeShop
 
{
 
    public string flavor;
 
    public int size;
 
    public float price;
 
}</syntaxhighlight>
 
 
 
Now, let's look at what our coffee shops can ''do''. Coffee shops can brew coffee, sell coffee, and our coffee! Let's give our CoffeeShop class the ability to do this by giving it the proper methods:
 
 
 
<syntaxhighlight lang="csharp">class CoffeeShop
 
{
 
    public string flavor;
 
    public int size;
 
    public float price;
 
 
 
    public void BrewCoffee()
 
    {
 
          Console.WriteLine("Coffee of the flavor " + flavor + " was brewed.")
 
    }
 
 
 
    public void PourCoffee()
 
    {
 
          Console.WriteLine("Coffee was poured into a " + size + " sized cup.")
 
    }
 
 
 
    public void SellCoffee()
 
    {
 
          Console.WriteLine("" + flavor + "  flavored coffee at " + size  + " size was sold for " + price + " dollars.")
 
    }
 
 
 
}</syntaxhighlight>
 
 
 
Objects can determine the behavior of the class they're created from. When you call an object in code, you can tell it to invoke one of its data variables or functions!
 
From a programming point of view, an object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed to be part of a class's hierarchy.
 
 
 
So, now that we've got our coffee class, let's finally create an OBJECT ([[Instance]]) of the CoffeeShop class!
 
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
Line 61: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
You declare these objects just like you would a variable! By using these objects, we can specify characteristics without affecting the CoffeeShop class. Let's do that now:
+
[[File:ObjectInstanceVisual.jpg | 400px]]
 
 
<syntaxhighlight lang="csharp">
 
starbucks.flavor = "vanilla";
 
starbucks.size = 3; //venti! just how I like my coffee.
 
starbucks.price = 3.20;
 
</syntaxhighlight>
 
 
 
Now we can call our instance "starbucks"' functions!
 
 
 
<syntaxhighlight lang="csharp">
 
starbucks.BewCoffee(); //outputs "Coffee of the flavor vanilla was brewed."
 
starbucks.PourCoffee(); //outputs "Coffee was poured into a 3 sized cup."
 
starbucks.SellCoffee(); //outputs "vanilla flavored coffee at 3 size was sold for 3.20 dollars.
 
</syntaxhighlight>
 
  
 
=Resources=
 
=Resources=
Line 87: Line 37:
  
 
==External Links==
 
==External Links==
 
+
* [https://whatis.techtarget.com/definition/instance Instance Definition at TechTarget.com]
 
+
* [https://www.techopedia.com/definition/16325/instance Instance explanation at Techopedia.com]
  
 
[[Category:Programming Language Concepts]]
 
[[Category:Programming Language Concepts]]
 
[[Category:Object Oriented Programming]]
 
[[Category:Object Oriented Programming]]
 
[[Category:C Sharp]]
 
[[Category:C Sharp]]

Latest revision as of 15:12, 12 August 2019

Definition

In object-oriented programming, an instance is an occurrence of an object. Each instance is unique and will have its own data. Creating an instance of an object is also known as instantiation.

Instance and object are synonymous in that they have specific values when they're created.

Relevance

Explanation

"Instance" emphasizes the distinct identity of the object, and instantiation is one of the core elements that make up object-oriented programming! Think of it this way - instances are the objects to which we orient our programming. Makes a lot more sense that way, huh?

Creating instances allows us to determine the behaviors of an object of the class they're created from. When you call an object in code, you can tell it to invoke one of its data variables or functions! From a programming point of view, an instance object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed to be part of a class's hierarchy.

Think of a coffee shop - there are a lot of different types of coffee shops around, but they each have similar characteristics. So, let's make a COffeeShop coffee class, and create an INSTANCE object of that class:

CoffeeShop starbucks = new CoffeeShop();
CoffeeShop dunkin = new CoffeeShop();

ObjectInstanceVisual.jpg

Resources

See also

Notes

External Links