OOP is difficult to understand because it’s an abstract concept that doesn’t really apply to anything within the bounds of reality. That may sound really out there, but it’s software, it’s a bunch of jibberish that a computer uses to take input and do something with it.
I have taught several people OOP and it always comes back to The Rocking Chair. I don’t remember where I picked this up, or if I made it up, but read on and you shall understand the basic concept and use of OOP.
“Everything in [c#, Java, etc] is an object…” you’ve heard it a million times. What does that even mean? Well try to visualize it in reality, what is an object.. well , everything is. A ball, a cow, a rocking chair. Let’s stick with a rocking chair.
Classes, Objects, and Properties
Let start first with a regular chair. We need to look at what makes a chair a chair, what are the properties of the chair. Color, material, height, width, weight, capacity, does it have armrests, a back? I will use C# to code all this because I think in C#.
To create an object we need something that defines what that object actually is, we use a class or a struct (structure) to define this. So let’s create a chair.
A quick word on properties and methods.. properties are the adjectives, methods are the verbs. Properties hold the information about an object, methods (or functions, routines, messages) do things, they make things happen.
[Chair.cs]
using System;
using System.Collections.Generic;
using System.Text;
namespace UnderstandingOOP
{
public class Chair
{
}
}
Wow, that was exciting.
Now, let’s add the properties of the chair that we recognized above.
[Chair.cs]
using System;
using System.Collections.Generic;
using System.Text;
namespace UnderstandingOOP
{
public class Chair
{
// these are the private members
// don't worry about these right now
// these are used so that we can save values
// and use them in our code.
private string _color;
private string _material;
private int _height;
private int _width;
private double _weight;
private double _capacity;
private bool _hasArmrests;
private bool _hasBackrest;
// these are properties. This is how we will access/set
// the information about a Chair when we create it
// these just protect the private members
// they restrict and enforce the way that a user
// can interact with the private members
public string Color
{
get { return this._color; }
set { this._color = value; }
}
public string Material
{
get { return this._material; }
set { this._material = value; }
}
public int Height
{
get { return this._height; }
set { this._height = value; }
}
public int Width
{
get { return this._width; }
set { this._width = value; }
}
public double Weight
{
get { return this._weight; }
set { this._weight = value; }
}
public double Capacity
{
get { return this._capacity; }
set { this._capactity = value; }
}
public bool HasArmrests
{
get { return this._hasArmrests; }
set { this._hasArmrests = value; }
}
public bool HasBackrest
{
get { return this._hasBackrest; }
set { this._hasBackrest = value; }
}
// this is a constructor, we need it, don’t worry about it.
public Chair() { }
}
}
Alright. So now we have something that defines what a chair is. So let’s create an object from that definition.
[myAwesomeClass.cs]
using System;
using System.Collections.Generic;
using System.Text;
namespace UnderstandingOOP
{
public class myAwesomeClass
{
// lets pretend this is the entry point of
// our application... where the application
// starts executing from.
public static void Main(string[] args)
{
Chair myChair = new Chair();
// lets assume height and width are in feet,
// weight is in lbs, and capacity is people
// that can sit in it.
myChair.Color = "green";
myChair.Material = "nerf";
myChair.Height = 5;
myChair.Width = 7;
myChair.Weight = 100;
myChair.Capacity = 3;
myChair.HasArmrests = true;
myChair.HasBackrest = true;
}
}
}
Awesome.. so we defined what a chair was, in our class, then we created an object based on that definition. We then set all the properties of that object to what we wanted so that we came out with a big green nerf chair.
Inheritance and Polymorphism (don’t be scared, that word means less than it sounds like)
Now that we have a chair, we can create a derivative of that chair. By using the basic OOP concept of Inheritance we can create another type of chair without having to redefine the underlying structure.. which is a basic chair. Rocking Chair needs to inherit everything that a Chair is and does.
public class RockingChair : Chair {}
TADA. We now have defined that a Rocking Chair is exactly the same as a chair. Using the above example you could now do RockingChair myChair = new RockingChair(); . But that’s not quite accurate is it.. a rocking chair does a lot more than a regular chair … plus is HAS TO have a back.
So we can extend the functionality of a Rocking Chair.
[RockingChair.cs]
using System;
using System.Collections.Generic;
using System.Text;
namespace UnderstandingOOP
{
public class RockingChair : Chair
{
// again.. private members
// they are here to keep things in sync between the methods
private bool _isRocking = false;
private int _rockingSpeed = 0;
// These are public methods. A method is a verb,
// it’s an action that your object is going to perform
// A property is a noun, they don’t DO anything,
public void BeingRocking()
{
// whatever code would make a chair rock
}
public void StopRocking()
{
// whatever code would make a chair stop rocking
}
public void RockFaster()
{
if(this._isRocking && this._rockingSpeed <= 10){this._rockingSpeed++;}
}
public void RockSlower() { if(this._isRocking && this._rockingSpeed >= 1){this._rockingSpeed--;} }
// property is now read only.. so it can’t be changed;
public bool HasBackrest{get{ return true;}} }
}
So using the example above…
[myAwesomeClass.cs]
using System;
using System.Collections.Generic;
using System.Text;
namespace UnderstandingOOP
{
public class myAwesomeClass
{
// lets pretend this is the entry point of
// our application... where the application
// starts executing from.
public static void Main(string[] args)
{
RockingChair myChair = new RockingChair();
// sweet now we have a RockingChair object!
// lets make our chair look cool by setting the properties
// lets assume height and width are in feet,
// weight is in lbs, and capacity is people
// that can sit in it.
myChair.Color = "green";
myChair.Material = "nerf";
myChair.Height = 5;
myChair.Width = 7;
myChair.Weight = 100;
myChair.Capacity = 3;
myChair.HasArmrests = true;
// myChair.hasBackrest =true; we can’t set this anymore, it’s always true.
// ok make it rock…
myChair.BeingRocking();
myChair.RockFaster();
// loop it to rock SUPER FAST!
for (int i = 0; i < 10; i++) { myChair.RockFaster(); }
//ok.. stop the rocking!
myChair.RockSlower();
myChair.StopRocking();
// methods always have to have () after them, sometimes
// you will pass parameters in the () like (“myName”, 1,2,3)
// etc.. but for methods like ours with no parameters, the ()
// is still required. Properties DO NOT require () as they
// are not methods and do not have parameters.
}
}
}
Awesome! So.. now you understand… difference in a class and an object, difference in a property and a method, and what they do, and the basic concept of inheritance!
Let’s look at polymorphism. Polymorphism is a complicated way of saying that you can create an instance of a class (object) and assign it to the base object. Weird… yeah.. here’s an example…
Chair myChair = new RockingChair() …. Spectacular. Not really. That’s polymorphism…
You could conceivably create an array of Chairs and add all kinds of different chairs to it (Rocking Chairs, bar stools, office chairs, etc) and it would be fine. That’s a really basic use of polymorphism. Wikipedia has a really good description of it.
Getting good at OOP will help you to write code quickly and reuse code as much as possible, even use code between applications. This was a very basic overview to help you understand what an object is, how it relates to a class, what a method and property is, and what inheritance and polymorphism are.
I hope it helped, email me or comment if you have questions!
All the code for this example is attached and downloadable, it's a VS 2005 project. Enjoy!
OOPChair.zip (21.92 kb)