esse quam videri
Revision as of 15:17, 12 August 2019 by Thorne (talk | contribs) (Explanation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Objects are instances of a particular class or subclass that is able to use that class's own methods or procedures and data variables.

In object-oriented programming (OOP), programs are designs with objects in mind - they are the centerpiece in constructing your units of code!

  1. The object is what actually runs in the computer.
  2. In Encapsulation, objects manage their own states through methods of its class type.
  3. Through Polymorphism, any objects or classes that inherit methods or variables from a parent are able to use them exactly like that parent class! Each child, however, keeps its own methods as they are.

So, child objects and classes can use these methods without affecting their parent class’s methods and variables.



Like a program or class type, objects have variables and method - but the added benefit of an object is that it can be invoked or instantiated like a method!

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.


Take a look at these links for some useful tutorials over how to use objects!

See also


External Links