Tuesday, May 24, 2016

An Introduction to Object Oriented Programming in Python - Part 6

Encapsulation, Inheritance and Polymorphism

Make sure you read Part 1, Part 2, Part 3, Part 4, and Part 5 before you continue reading...

These are considered to be the three pillars of OOP. However, some authors may include Abstraction, Association, Containment etc as part of the OOP pillars.

Encapsulation hides the implementation details of a class from other objects.

Inheritance is a way to form new classes using classes that have already been defined.

Polymorphism is the process of using an operator or function in different ways for different data input.

Abstraction is simplifying complex reality by modeling classes appropriate to the problem.


Lets explain one after the other starting with Encapsulation.


Encapsulation in Python OOP

Encapsulation is also called "Data hiding" 

Encapsulation in an object oriented python program, prevents the data from being modified by accident. That is you can restrict access to methods and variables, this is known as data hiding (Encapsulation of data).

The terms encapsulation and abstraction (also data hiding) are often used as synonyms. They are nearly synonymous, i.e. abstraction is achieved though encapsulation. C++, Java, and C# rely on the public, private, and protected keywords in order to implement variable scoping and encapsulation

Generally speaking encapsulation is the mechanism for restricting the access to some of an objects's components, this means, that the internal representation of an object can't be seen from outside of the objects definition. 

Encapsulation gives you more control over the degree of coupling in your code, it allows a class to change its implementation without affecting other parts of the code.

Let’s start with an example.

To achieve Encapsulation in python, we make Protected and Private data (attributes and methods) since by defualt they are public.

Public, Protected and Private Data

Attributes and methods in python OOP are either Public, Protected or Private.

A Public attribute or method is usually written without starting with an underscore character "_". It can be accessed from outside the class, i.e. the value can be read and changed.

A Protected attribute or method is created by starting its name with one underscore character  "_". Protected members can be accessed like public members from outside of class, but they are NOT meant to be so.

A Private attribute or method is created by starting its name with two underscore characters  "__", its value cannot be accessed/changed from outside of the class. The private attribute or method can only be called within the class itself.  It can never be called from outside the class.

The following table shows the different behavior Public, Protected and Private Data


Now lets write some code to demonstrate all we have said above about Public, Protected and Private data (attribute and method) of a class in python.

Recall our Cars class from previous parts... all the attributes (brand, speed, height, weight, colour) and methods (activateHorn, moveForward, moveBack, turnRight, turnLeft) were public.

Lets make some Protected and Private data as follows:-

Attributes
Public = brand
Protected = _speed, _height
Private = __weight, __colour

Methods
Public = activateHorn
Protected = _moveForward, _moveBack
Private =  __turnRight, __turnLeft

Our code should look like this:-

class Cars:
# Create and Set the class attributes using constructor (__init__) method
 def __init__(self, brand, speed, height, weight, colour):
  self.brand = brand
  self._speed = speed
  self._height = height
  self.__weight = weight
  self.__colour = colour
  
# Create the methods with simple "print" statement in them
 def activateHorn(self):
  print ( self.brand + " Says: Poooooooorh!")

 def _moveForward(self):
  print ( self.brand + " Moving Forward at the speed of: " + self._speed)

 def _moveBack(self):
  print ( self.__colour + " " + self.brand + "  is Moving Backward!")

 def __turnRight(self):
  print ("Turning Right!")

 def __turnLeft(self):
  print ("Turning  Left!")


# Creates First object car1 and Assign the attributes value of car1 object
car1 = Cars("Toyota", "250km/hr", "1908mm", "2800kg", "Green")

# Accessing the attributes
print car1.brand
print car1._speed
print car1._height
# print car1.__weight
# print car1.__colour

# Accessing the methds
car1.activateHorn()
car1._moveForward()
car1._moveBack()
# car1.__turnRight()
# car1.__turnLeft()


Note that: Public, Protected and Private data can be accessed within the class while only Public and Protected data can be accessed outside the class definition.

That is it.

In the next Part 7, we will discus Inheritance in python OOP.


No comments:

Post a Comment