Object-Oriented Programming
By: Melody Soriano
Tags: Programming, Object Oriented, C Sharp Programming

Let’s start at the very beginning. The first concept to think about is objects and classes. There are two ways that I personally like to think about objects: as a bucket of variables and functions (or methods) and also as an instance of a class. Now, if an object is one specific thing, then a class would be the category that thing belongs to. Consider this: I have two cats, Marble and Cookie. They are… decidedly not the same cat, but they are both cats nonetheless.

Marble (left) and Cookie (right) {follow on insta @ marblecakethecat}

In this example, the class would be Cat, and the instances of that class, a.k.a objects would be the two cats Marble and Cookie. If we suppose that the class Cat has attributes (variables in its bucket) of name and age, this would look like so in code:

There’s a lot of stuff up there that we don’t need to worry about right this minute. The most important part of the snip above is the lines that start with Cat. There, we are creating two variables of type Cat that are called marble and cookie, and we are setting their respective names and ages. Then for funsies, we can write both of their names out to the console! Let’s take a look at the Cat class and see what we can get out of it.

Okay, on the left we have a lot of fun things we can look at here! The first thing you should notice is the fancy colon after the class name Cat. This, in C#, tells us that the Cat class is inheriting from the Animal class. What does that mean? Well for one, notice that age and name aren’t defined here in Cat. That’s because they are attributes that Cat is inheriting from its parent Animal. We also haven’t explicitly defined Set/GetAge or Set/GetName here, because they belong to Animal also.

Now, what’s up with this Set/Get nonsense? We have it for our breed attribute as well! Why can’t we just access our variable directly? We can’t do that because of the fancy private keyword in front of our variable name. These access specifiers are how C# implements the second principle we’re touching on today: encapsulation! This is the process of enclosing our variables within a logical package. Our class is public because I’m accessing it from another project within my solution. Our instance variables are private as is best practice, and then accessed /changed with getters and setters. I could, for instance, make the breed variable read-only, in other words, I could delete the setter method.

Encapsulation here is working hand in hand to abstract our data and put it more or less in a need-to-know situation. Abstraction here also allows us to have our classes act as a black box of sorts, letting us use them, without us needing to know how the inner workings of our class actually function.

The last fun thing you can notice is that we have two constructors for our cat class, one with two required attributes and one optional, and one constructor with no attributes required. This is a great example of the last principle we’ll cover in this article, polymorphism. Polymorphism allows us to have multiple functions with the same name that take in different types of arguments or different numbers of arguments or even do totally different things!

Well, there you have it. Object-oriented programming more or less in a nutshell. I will leave you with the fun little console app I ended on with this class: