Difference between revisions of "Object"

esse quam videri
Jump to: navigation, search
(Definition)
(Explanation)
 
(18 intermediate revisions by one other user not shown)
Line 4: Line 4:
 
    
 
    
 
In object-oriented programming (OOP), programs are designs with objects in mind - they are the centerpiece in constructing your units of code!
 
In object-oriented programming (OOP), programs are designs with objects in mind - they are the centerpiece in constructing your units of code!
The object is what actually runs in the computer.
+
# The object is what actually runs in the computer.
 +
#In [[Encapsulation]], objects manage their own states through methods of its class type.
 +
#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.
  
 
=Relevance=
 
=Relevance=
 +
*[[Programming Tutorial: Classes Part 1]]
 +
*[[Class]]
 +
*[[Instance]]
 +
*[[ Inheritance]]
 +
*[[Encapsulation]]
 +
*[[Method]]
 +
*[[Variable]]
  
 
=Explanation=
 
=Explanation=
 +
Like a program or class type, ''objects'' have variables and [[Method|method]] - but the added benefit of an object is that it can be ''invoked''  or [[Instance|''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!
  
 +
[[File:ObjectInstanceVisual.jpg | 350px]]
 +
 +
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">
 +
CoffeeShop starbucks = new CoffeeShop();
 +
CoffeeShop dunkin = new CoffeeShop();
 +
</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:
 +
 +
<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=
 +
Take a look at these links for some useful tutorials over how to use objects!
 +
* [https://www.guru99.com/java-oops-class-objects.html Class Objects through Java]
 +
* [https://stackify.com/oop-concepts-c-sharp/ OOP Concepts in C#]
 
== See also ==
 
== See also ==
 
* [[C Sharp Data Types]]
 
* [[C Sharp Data Types]]

Latest revision as of 15:17, 12 August 2019

Definition

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.

Relevance

Explanation

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!

ObjectInstanceVisual.jpg

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

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

See also

Notes

External Links