Thursday, 23 January 2014

Week 3: Object-Oriented Programming

OOP ( Object-Oriented Programming)

The OOP ( Object-Oriented Programming) is a model language programming, which is structured by creating classes to abstract something that exists in the real world. So, the classes makes possible to create a customized type of data, and this can be used to create a new 'Object' which has the type of the data (class) allocate in the heap. Each class has their own methods (functions that add the class's behaviors) and attributes (variables that store information which the class need to work properly). Another benefit of OOP is making the concept of recicle code. In the past, the structured programming was used to create softwares, but this model had the main problem which was the maintenance of code. When some function get some bug, the programmer had to change in every single line that used the bugged function. Then, the OOP came and makes the code more clear and easier to fix because the class bugged is the most cases in one single file, so the programmer just need to access the file and make the appropriate updates. Also, the class makes the program shorter because the developers can create as many instances of the determinate class as they want. The inheritance is another feature of OOP. The concept of inheritance determinate that you have a superclass, which is a general class, and their subclasses. The subclasses inherit all the methods and attributes of his father (superclass), and they can implements new methods and attributes to make the subclass more specific. If is necessary, the developer can overwritten in the subclass the methods that superclass implements.

Since I have already studied OOP in my hometown university in Brazil, I used a simple examples to easily linked the concepts of OOP with real life. The developer wants to create a class called 'Animal', in these class, and he/she want to add characteristics and the common behaviour or actions of animals. So, the first step to create a class is to define what is method and attributes. In this particular case, if we think all the physical characteristics such as eyes, color, age, and weight are attributes of the class 'Animal'. The common behaviour or actions such as move, feed, and sleep  are the methods of 'Animal'.  Inside the class 'Animal', if the programmer want to call one method inside the other method or use an attribute, in Python, he/she need to use the reserved word 'self' which identify that is bellow of the class. For example, if we want to call the helper method 'bite' inside of method 'feed', the line has to be similar with ' self.bite()', in our examples, we can read the same line as 'Animal.bite()' because the 'self' is used to replace the class's name. After you define all the methods and attributes, it is possible to create an object of the class 'Animal'. Because of 'Animal' it is too general class, we want to create two subclasses called 'Lion' and 'Bird'. In these situation, we use inheritance because we want the attributes and method of 'Animal', but at the same time each animal has particular attributes and methods, so we create a class with line ' class Lion (Animal) '. For checking if the relationship is correct, the question 'is a'  must be true to 'subclass is a superclass', in our example, 'Lion is an animal' make sense, but if we say 'Animal is a Lion' this statement does not make sense. In some cases, you need to overwrite the method of superclass, because the subclass have the same action (method), but the behaviour is completely different. If we think in the method 'feed'  both lion and bird feed, but the birds, unusual of the rest of animals, 'peck' instead of 'bite' their food. In order to solve the problem, the programmer must overwrite the method  of 'Animal.eat()' inside of the subclass 'Bird'. That was a simple example that I create in my brain to understand better the lectures that professor Heap is given to us and describe what I have been learning in the CSC 148.






No comments:

Post a Comment