Object oriented programming or OOP is a new concept for me. Till now, all I had done was C and some scripting languages. All of them were ‘procedural’ type of languages. I knew nothing about OOP, and I really hate knowing nothing about anything. So I decided to try it out. I could learn or start with C++ or Java, but I choose Python. The reason being its flexibility and more importantly, it is a language of choice when it comes to creating simple utilities and exploits, which is a field of interest of mine.
So it went good. I studied Python, both in procedural and object oriented way. I am still a beginner in Python but I studied the OOP concepts well. Then my college started and I had a subject called OOPM which deals with the same OOP stuff plus Java. The teacher, who teaches us the subject, I don’t really know what is wrong with her, but her examples are only confined to banks and ATM machines. That is pretty funny and annoying. One example is good, two are Okay, but when someone goes on like a week entirely on the same examples to teach something like OOP, your brain dies.
I have attended all the lectures and I am still trying to figure out what exactly she teaches during the lecture. Just a reminder, I have recently finished with OOP concepts in Python, so it shouldn’t have to that hard. Then I can only imagine the state of all those who are actually doing it for the first time.
Sick of it, I am writing this post on OOP concepts and will try my best to keep the terms and explanation simple and practical.
Starting with Object Oriented Programming
OOP, as the name suggests is Object Oriented. Meaning that objects are the significant em…objects in it. But what exactly are objects? I wont answer it right away, because it would require some understanding. For now, everything, from variables to functions and else, are objects. First we will see some of the terms used.
A class is like a code-template for creating objects of similar types which have some dissimilarities. For example, If I had to classify the Animal Kingdom again, I would create a class called animals which will be the parent class.
dog = Animals()
dog.legs = 4
cat = Animals()
cat.legs = 2
And to use the class, we simply create an instance of it, which can be called an object as well. Dog and Cat are the instances here, for example. Since the class is empty (see the ‘pass’). We can set attributes to the instances. ‘legs’ is an attribute to the object dog and cat, which are instances
Objects are all around you. Take a look. Everything that can have an individual significance can be considered an object. All objects have two characteristics, state (like color, size) and behavior (like tasks it does, eating, sleeping). In programming, objects are much similar to that. They have state (which are variables) and they have behavior (which is through methods). In the above ‘class’ example, cat and dog are objects. Let me write something that will make it clear.
self.x = x
self.y = y
def area(self, x, y):
return x * y
rectangle1 = Foobar()
area = rectangle1.area(4,6)
So rectangle1 is the object I created. I use the method area to find out area of the supplied rectangle.
Inheritance is using an existing class and creating a new class which inherits, or has all the features of the old class plus some new features. So, if one were to organize reptiles and cats separately, he could do this.
This creates classes which can be called daughter classes of parent class Animals. They inherit all the properties of Animals plus they can have their own new properties. It results in code reuse, but read any good book and they will tell you why you should avoid inheritance. Since this is not a guide, I wont.
When you write a function inside a class, it is called a method. Why? I don’t know. Some books I have read insisted on calling them functions, while some didn’t. Its upto you. For the sake of an example, I will write a class ‘Shape’ and daughter classes and methods in them for finding their respective areas.
def area(self, radius):
return radius * radius * math.pi
def area(self, base, height):
return 0.5 * base * height