Difference between revisions of "Instance"

esse quam videri
Jump to: navigation, search
(Explanation)
(Definition)
Line 1: Line 1:
  
 
=Definition=
 
=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.
+
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 [[Instance|instantiation]].
  
 
=Relevance=
 
=Relevance=

Revision as of 00:04, 10 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.

Relevance

Explanation

Instances are concrete occurrences of objects, for example of a class. Instance and object are synonymous in that they have specific values when they're created.

"Instance" emphasizes the distinct identity of the object. The creation of an instance is called instantiation.

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!

Let's create a 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!):

class CoffeeShop
{
    public string flavor;
    public int size;
    public float price;
}

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:

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.")
    }

}

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!

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

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:

starbucks.flavor = "vanilla";
starbucks.size = 3; //venti! just how I like my coffee.
starbucks.price = 3.20;

Now we can call our instance "starbucks"' functions!

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.

Resources

See also

Notes

External Links