Saturday, 28 February 2015

Summary of Object Oriented Programming concepts ORIGINAL

I've edited and re-edited my "summary of object oriented programming concepts" blog post so many times, it's practically an entirely new blog post. So, I thought it would be an interesting idea to publish the original version. I hope you enjoy it!


I) Objects:

Perhaps the most important concept of object oriented programming is the titular objects themselves. Most python textbooks or websites will define an object as being a representation of a value, with a memory address that points to that value somewhere in the magical world of computer memory. For instance, the number 4 is an object. The memory address to the value of 4 is the same regardless of how we are pointing to that object, and any variable that refers to the object 4 will be considered identical (since they all point to the same value in computer memory, regardless of how that 4 was obtained).

>>> s = 2+2
>>> t = 4
>>> s is t

Note that the value to certain objects is not always the same. The mutable types, such as list objects, are a prime example. Even if we assign 2 variables the same value - an empty list- they will not be considered identical:

>>> t = []
>>> s = []
>>> s is t

Although its not obvious at first, the reason this happens is because the empty list s refers to is actually a different empty list than the one t refers to. If something is added to one, it will not be added to the other. However, they are still considered equivalent, as:

>>> s == t

As much as I love rambling about objects, I need to cover a few other concepts first. These concepts will incorporate and extend the concept of objects.

II) Classes:

A class is usually defined as a blueprint for building an object. Now, remember how we defined objects as values with associated memory addresses? Well, lets expand on that a little. Let us say that we are using a programming language which, somehow, has no classes. In this language, we could type in a sequence of alphabetical characters which just so happen to spell out the phrase "brown_nosing". Now what? Well, we could store it in a variable- but that's not very interesting. We could try to count every character in the sequence, or to return a copy of it in all caps. Perhaps we want to add the phrase "I_am_a" to the front of it. But how do we do those things? And worse off, what if we wanted to do these things to the phrase "you_are_an_amazing_person"? We would have to a lot of retyping, wouldn't we? However, what if we could create an infinite amount of sequences of alphabetical characters, which all have the same sorts of properties? Well, that's what classes are for. With a class like, oh I dunno, strings, we can create individual "string" instances, of which all share the same sorts of properties. For instance, whenever you put two strings together with a ' + ' in between, the two strings will be combined into one. This works with all strings, so now we can add the phrase "I_am_a" to "Brown_noser" or to "fan_of_you", without needlessly retyping things.

So, do be succinct, a class defines what all its members can do. And now to transition to a few more OOP concepts.

III) Properties of Class members

Now, what do I mean when I say that class members have properties? Well, think about how things in the outside world have properties. Consider an elephant. An elephant has things it can DO - such as  blow water out of its nose/snout thing. It also has things that represent what it IS- for instance, it has 4 legs and 1 long nose tubey-thing. Now, suppose we want to create a bunch of virtual elephants in our computer (because hey, Watson wrote a cook book so, why not?). If we make each elephant an object, which is a member of a class, than we need to have some way how the class determines what each elephant instance can do and what properties it has. These are the roles of methods and instance variables, respectively. Methods can be thought of as "instance functions", since they are functions that exist inside of every object which is a member of the class the method was defined in. Instance variables function in the same way, except that, for every property, it can either be a class variable (in which ever member of the class shares the variable with the exact same shared value initially), so they can be initialized- that is, the value of the instance variable is set as soon as an instance is created. How you chose to use both types of instance variables will depend of the type of problem you are trying to solve.

IV) Inheritance of properties

In much the same way that an object is an instance of a class- a class can also be an instance of another class. When this happens, some of the properties of the superordinate class can be shared by the subordinate "children" classes. This is inheritance.

V) Implications of the Instantiations of Classes:

Now, this is the last thing I want to cover. There are very technical terms I could also cover, such as the concept of property and the distinction between extending and overriding, but, in my opinion, they are really just variations of the same abstract concept of instantiation. When you instantiate, you create an object which is an instance of the class. This part should be clear by now. But think about the implications for a moment. This means that often, there is a one way relationship between class and the instance of the class. Although if may be the case that, if the object is an instance of class A, it gains such and such properties- having such and such properties isn't proof that all members of the class have those properties. In fact, often, we want to add to the properties of an object after it has been instantiated. The object does not stay the way it was initialized- in fact the reason it was initialized the way it was was to make modifying it easier.

The same thing applies to subclasses. In fact, you can completely override the properties of a subclass so that it functions completely differently than other subclasses of the original class (or the original class itself). Similarly, you can merely add properties to it- which is extending. In the end, whichever one of these techniques you utilize will depend on the problem at hand. In the workforce (from what I've been told), it is considered bad form to just completely change you code if other programs are working with it. In these cases, you would want to utilize extension, since the prior code would continue to run as intended, while newer code could utilize any of the newly added functionality.

Anyways, I hope that this was a suitable summary of some object oriented programming concepts. I wanted to cover what, in my opinion, was most fundamental to understanding the OOP philosophy, without going over too much. Well, that and I've been typing this for an hour, so now I need to sleep.

No comments:

Post a Comment