Sunday, October 27, 2013

Programmer Life: An Introduction to Object Oriented Programming

Lately, I've been talking to a lot more people who are opting out of the traditional 'let's go to college and get a Computer Science degree' and, instead, teaching themselves how to program. For someone who learns quickly, is highly motivated, and has a strong interest in the subject, this can be an excellent route to take and can get you into a working position in the industry remarkably quickly.

So I've decided to start a biweekly column on this blog to contribute to that effort. The column will be called, as you might have already guessed, "Programmer Life" and we're going to cover all of the fundamentals you need to know in order to become a working programmer, broken down into short, easily understandable chunks.

This week, we're going to start discussing one of the most widely used methodologies in the modern software development world: object oriented programming. You've probably heard that term before and it might have confused you. Let's see if we can take away some of that confusion.

First, let's start with a basic truth: everything is either an object, a property, or a method. For the most part, that can be extended even into real life. Think about that for a second. Think about your car (an object). It has properties (color, make, model, etc) and  methods (go forward, turn left, etc). So how would we represent a car in code? Probably much like this: 

public Class Car{
    public int yearMade;
    public String make;
    public String model;
    public String color;

With the code above, we have everything to need to describe any car in code because all cars share a common set of attributes. So, how to we represent a Black 2014 Ford Fusion using this code? Pretty easily:

Car myCar = new Car();
myCar.yearMade = 2014;
myCar.Make = "Ford";
myCar.Model = "Fusion";
myCar.Color = "Black";

Pretty easy, huh? We could just as easily represent a 2013 Chevy Malibu or a 1978 Grand Am. Because all cars share these common properties, we don't have to rewrite the same code for every single vehicle we define. We simply define a new instance (called instantiation) of a class of object we've already defined.

So that's a quick and dirty introduction to OOP. In our next column, we'll discuss how we make our objects do something using methods.  Until then, go play around with code!

No comments: