First of all, I’d like to wish you to have a wonderful holidays with your family, wherever you are at this moment. During the first lectures, we went over some of the basics of programming. Even though it might have seen like a lot, it was just the core fundamentals we needed to understand the basic logic in any given program. Don’t worry, we’ll cover some more later in the blog. Right now, with what we’ve seen so far, I thought it would be a good idea to get into the hype of object oriented programming.
Say what? Object oriented programming (OOP) ? Sounds funny huh? It is actually a really popular programming paradigm in the developer’s world. It is my belief that we ought all to know more about this. I don’t mean that we need to be expert in this particular field, but there’s so much to do with this paradigm that it would be a shame to not exploit what it gives us !
To begin, what is oriented object programming ? Wikipedia defines object oriented programming as the following: “a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.” I’m going to roughly summarize this as a programming paradigm which able a given programmer to place any form of data in an “object” which can be use at anytime for any given purposes such as interact with it.
That’s some heavy abstract notions that was written. In this blog, I’m going to cover as much on this paradigm as I possibly can. OOP has a lot of advantages such as making code more flexible and more maintainable. As much as I would like to tell you that oriented-object programming is the best way to do things that you’d like to do, it is not always the case. In fact, like anything else this world, OOP has some downsides that can be unappealing like the effort it takes. To be really good in this paradigm, it takes a lot a thinking and creativity to create efficient objects which will help you reach your goals. Don’t get me wrong, I like OOP. Even if it takes some time and creativity, you must keep in mind that in any given programming paradigm, coding is a craft that takes time, no matter what you do.
Let’s head back to what OOP actually is. We saw some core basics that we all ought to master if we want to be good, or should I say, great programmers, such as loops or conditional structures (ex: if-else). With these, we can now move to the next level, in this matter, OOP. In OOP, we define virtual objects in our software. Most of the time, we try to represent real objects from the world such as a car or a an animal (ex: a dog). More than just saying this an animal, we give this object specific behaviors with the help of methods. Finally, at any time, an object can have a state too, such as being awake or tired in the case of a dog. To wrap up, in order to create an object Dog, we would have to define what is called a class named Dog which would be define with different attributes like a weight (double), a name (string) and a number of legs (int). To interact with this dog, we would have to define the different behaviors we expect to find in this object such as: walking, barking or sleeping. You’ll be able to see below an image that I found on the web, which sums up what I’ve said
In the previous paragraph, I have mentioned a specific keyword of oriented object programming : class. A class is a template in which we will store all the data we need in order to define the object we want to create. That’s all there is to it. When we use objects from this class, we call them instances of the class. When I say instances I mean that once you have created an object of a particular class, you actually are creating a reference which is pointing to the data of this specific class. In short, in your code, you can have multiple references to the same class (instances) but, that does not mean they will contain the same exact data. Let’s say that you’re creating a zoo application. In a zoo, there is more than one animal of the same kind, but they aren’t identical. In the same with objects. Some can be identical. for given purposes, but some are simply not.
While we’re diving in the oriented object programming paradigm, we’ll get to see what truly makes a programming language like C# OOP with the help of encapsulation, inheritance and polymorphism. Without those three core fundamentals, you cannot use OOP with a language. But more on this in a later post.