Sunday, March 1, 2015

Object-oriented Programming summary

Python is designed as an object oriented Programming language. Object-oriented basically means that  instead of using the object merely as the carrier of information we put the object at the centre of the process of programming. When we talk about the object-oriented approaches, first we define the objects and how they are related to each other. An object can be all sort of things. It can be a person, a company, an animal, etc. secondly, we add methods to manipulate the data the objects represent.

First of all, new classes need to be introduced to represent the properties the object possesses. For example, we can define a new class called pet which has variables name and age. We can then create an instance called d1 and give the variable name mike and age the value 3. d1 is an object with the name and age of a certain pet.

After a new class is defined, we have to add some methods to manipulate more complicated data. A method is a function contained in the class. The difference is that when using a method we have to refer to an object in the class to which the method is belong first. For example, we can add a method called add_legs for the class pet which adds the number of legs of  the pet.

Moreover, we can define a subclass of the parent class which inherites all the features the parent class has. For example, to represent a more detailed object, we can add a subclass dog to the parent class pet to extend the class. Class dog inherits the methods and the variables pet has and in the meantime possesses more methods of its own. We define a subclass instead of defining a new independent class dog and getting rid of the class pet mainly for two reasons. One is that we want to keep the parent class just in case I want a generic pet that is not a dog. Two, I can add more kinds of pets later in the parent class pet like fish or cat and they can also inherits the features of a general pet.

Above is a quick introduction of object-oriented programming.

No comments:

Post a Comment