There are lots of Programming paradigms out there. Where a paradigm describes what the syntax and semantics of a language can do.
I talk about object orientation (referred to as OO) because it is the most prevalent paradigm as far as I can tell, and C# is mostly an OO language.

So what are we going to tackle in this post?

  • What does Object orientation mean?
  • Why would you want Object orientation?
  • How do programming languages achieve object orientation?
  • What are the benefits and drawbacks of OO on a high level?

What does Object orientation mean?

When you write a program to do stuff, you must represent the things you want the machine to interact with. We will investigate this relations and interactions with a character of a role play video game. Which we call Carl. Carl has some hitpoints, some attack points and lives in a world where he has enemies.

The good thing about object orientation is, everything can be an object. And the bad thing is, everything can be an object.

That makes it slightly hard to grasp, because saying everything is an object, does not say much. So how go we about making it more specific?

Why then would you want object orientation?

The term object in and of itself, does not help us to understand it further. So how can we specify what we mean with an object?We can specify an object with respect to what it is made of.

An object in terms of OO is made of its state, and its behavior. Sounds…fun? Lets look at Carl in this terms, his hitpoints and his attackpoints are his state, ok that makes sense. But what is his behavior? We can think of his behavior as him moving around the world, or attack an enemy.

The same applies for Carls surroundings. So every object around Carl is a specific object with state and behavior. Now Carl can interact with his surroundings and vice versa. So with sepcific objects it gets a little clearer, if not go ahead and grab a coffe cup object, which state is defined by a hot fluid that makes your brain comprehend all of this text-objects.

We can now differentiate objects by this specification, which is then called the type of this object.

How do programming languages achieve object orientation

In a programming language you need first to define the specifications of an object so the machine learns how to represent it.
Therefore you need to write a blueprint or recipe for an object. This recipe is called a class.

From this recipe an object can be created. If you create the object during the execution of your program, this object is called an instance of that class.

Wait, wait wait. class object, instance, type who and what is whom. Lets look at Carl again, shall we?

Carl is the instance because we want to represent the character in the running game. To get hold of Carl we must create him first (instantiate). Therefore we need the recipe. Which is his class. This class we call the VideoGameCharacter-class. VideogameCharacter is now also his Type. it specifies his behavior, and the state on which other objects in his world can interact with Carl.

So you can say, classes are known before you start the program, and as long as the program runs. An instance is only available during the time the program is running, and the Type of the instance refers to the class it was created from.

In C# code it would look like this
Recipe:

class VideoGameCharacter
{
int hitpoints = 100;
int attackpoints = 10;
void Attack()
{
}
void Move()
{
}
}

We will talk about the syntax of defining a class, its state and behavior in future posts.

What are the benefits of OO?

One of the prime benefits of OO is that you can think specifc objects that represent real life constructs. This allows for abstraction of interactions, and also allows to create a model of the real world problem you are trying to solve.
It also gives the code some structure, because you can organize things together, that belong together.

Also you can write code once and reuse it in other places. For example we could make another role play videogame, and use the same VideoGameCharacter class. But now we would call the instance Lrac ( 😉 ).

In future posts we will see that object orientation has a lot more to give. Which on the one hand extends those mentioned benefits, and makes programming with objects extremely versatile. On the other hand it makes it slightly more complex to work with the OO paradigm.

What are the drawbacks

Programming with objects is not without drawbacks. You need to wrap your head around thinking in terms of objects and their relations and how you model this in classes.

Also you need to distinguish classes from its instances. Because one class can have many instances that are of the same type, but might have different states during the application. Think of Carl, and another character that is called Carl II. Where as Carl was already hit by an enemy and has only 60 hitpoints left. And Carl II still got all of his hitpoints. So they have the same Type, yet different states. (this is in itself not a drawback, but it might be hard to grasp for a beginner)

At sometimes an object can mean too much overhead for what it accomplishes. Yet because C# and Python use lots of other concepts that are not generally thought of as object oriented this is seldom a problem. (at least for me)

If one does not understand object orientation, or does not design a system with some OO principles (which we will explore in future posts) in mind, it can add to quite a complex system with interactions that are unpredictable and unmaintanable as a system.

Often you come across similar problems with the interaction of objects. Those have solutions to it that are called design patterns. Yet some say those are only necessary because object orientation can not deliver a simple solution to those problems.

So you see, you need to learn object orientation as well as syntax of a language…You need to learn a lot of stuff simultaneously.

where do we go from here?

In the next posts on object orientation we will learn about:

Summary

So object orientation tries to model the real world with the aid of something that is totally anambigously called objects.

Those objects are made up of state and behavior to define their interaction with the world. Which obviously makes the matter a whole lot more clearer *rolleyes*.

As a beginner it is quite helpful to think of objects in terms of little units to organize your code. So the code to run your program can have structure with behavior and state that logically belongs together.

Where do we now get such an object? We build them from recipes. And one recipe is good for as many objects as you want.

Are interactions between objects also objects? Yes because everything is an object. As you can see OO programming is a piece of cake.

We will dive deeper into the world of objects and their interactions in future posts.

Categories: OO-Concepts

0 Comments

Leave a Reply

Avatar placeholder