Saturday 28 February 2015

Object Oriented Programming Summary

When we have multiple 'copies' (or instances) of an object, for example an animal, we can use OOP as a way to store each of the data found in each of the objects. We would first create a class which is basically a general 'blueprint' of something whose attributes (or variables) and behavior (which we dictate with methods) are generally defined. Every single instance of this class will contain its own 'version' of this code. For example all animals would have characteristics such as age, height, etc, and have certain general actions such as 'make animal sound' or 'defecate'.

Inheritance
But when we get into more specifics animals, such as a bird, it would undoubtedly still have the characteristics that apply to all animals so there's no need to rewrite the code. In this case we can create a child or subclass of the parent class which would inherit (and be able to use) all the attributes and methods that were made in the parent class. It would extend the parent class. By doing so, we are able to reuse code we have already written which is a key aspect in OOP.
___________________________________________________________________________________
But in this more specific case of the general class, we would have additional variables such as flight speed and methods that are exclusive to this form of animal. We are able to simply add their own behaviors and attributes that are unique to a certain animal. When an instance of a bird is created, it will be BOTH an animal AND a bird and can function/change independently of every other animal or bird too.

Encapsulation
Regarding encapsulation, it is generally used in order to prevent unwanted modification and access to certain parts of our code. In java we would denote variables as private instead of the default public and use accessors and mutators to control how we want our attributes be seen/changed.

But in python there is not true sense of 'private' (Although we can, by convention, use underscores in front of the variable/method name as a representation to others what we don't want them to access). Instead we can use the property() method to control under what conditions we allow a variable to be changed/modified.

Abstraction
Furthermore, in the parent classes we can manage/influence what is required in all of its subclasses. It will create a common interface for all the subclasses that are created so they can all be managed easily. Sometimes it's as simple as denoting a method as abstract or in python we can write the name of a method that we want to be in all subclasses and raise NotImplementedError.

In general Object Oriented Programming allows us to easily create multiple distinct instances of objects that are similar to one another but each contain their own unique characteristics. We could have a single bird or easily initialize multiple instances of birds to create a whole flock of birds with varying ages, feather colours, etc; this allows for easier manipulation of your code and all your objects.

No comments:

Post a Comment