Object Oriented Programming Part 1: What is an Object

Object-oriented programming, otherwise known as “OOP”, is a programming paradigm that breaks things down as objects, which have methods, fields, and member variables. Classes are typically represented as objects.

The best way to get a handle on what “OOP” is is to take a real-world object and define its properties. For example, take the chair you may (or may not) be sitting in. That chair has properties: size, color, height, width, type, etc. In “OOP”, you could define a class named “Chair” that contains those properties.

When an instance of the Chair class is instantiated, that instance is an object. The class itself is not an object.

A class is the code, an object is an instance of a class.

Let’s see some actual code that demonstrates this concept.

In Visual Studio, select File > New Project.

image

Select the Console Application Project type and give the project a name. I named mine “ChairConsoleApplication”. You can name your’s whatever you like.

Right-click on the new project in the “Solution Explorer” window on the right side of the screen. Select Add > Class.

image

Name the new class “Chair.cs” (without the quotes, of course).

The code below shows the implementation. Feel free to copy/paste this into their respective classes and change things up!

Forgive some of the code formatting issues. I’m using the “Insert Code” plug-in in Windows Live Writer to insert code snippets and it doesn’t carry over well when I publish to wordpress. If anyone knows of a better plug-in, please let me know!

Below is the Chair class with a few basic properties that all chairs have:

using System; 
using System.Drawing; 
using System.Web; 
namespace MyChairConsoleApplication 
{ 
class Chair 
{ 
public string Type { get; set; } 
public int Height { get; set; } 
public int Width { get; set; } 
public Color ChairColor { get; set; } 
} 
}

An instance of this class is declared in the Program.cs class that is generated when creating a new console application

This code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace MyChairConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var myChair = new Chair();
            myChair.ChairColor = Color.White;
            myChair.Height = 20;
            myChair.Width = 15;
            myChair.Type = "Office Chair";

            Console.WriteLine("The chair is a {0}. It has a {1} color, is {2} inches tall, and {3} inches wide.", myChair.Type, myChair.ChairColor.Name, myChair.Height.ToString(), myChair.Width.ToString());
            Console.ReadLine();
        }
    }
}

That’s a simple implementation of an object using properties.

In the next post, we’ll talk about classes in depth.

I hope you enjoyed this post. If you have any questions, comments, or suggestions, please do not hesitate to let me know in the comments!

Advertisements

One thought on “Object Oriented Programming Part 1: What is an Object

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s