Showing posts with label oops python. Show all posts
Showing posts with label oops python. Show all posts

Friday, October 2, 2015

Python: Encapsulation

Sample program

class Animal:
    def __init__(self, Animalname):
        self.__Animalname = Animalname

    def name(self):
        return self.__Animalname



x = Animal("This")
print x.name()

t = Animal("Pet2")
print t.__Animalname


Output:

C:\Python27\python.exe E:/Scripts_to_learn/Scripts_to_learn/Encapsulation.py

This

Traceback (most recent call last):
  File "E:/Scripts_to_learn/Scripts_to_learn/Encapsulation.py", line 17, in <module>
    print t.__Animalname
AttributeError: Animal instance has no attribute '__Animalname'


Notes:

1. To make the attribute private, use "__". So that it can't be accessed from outside
2. To know/use the private attribute, create a method as above (Eg: Above 'name' method)



Python polymorphism

Note
http://stackoverflow.com/questions/3724110/practical-example-of-polymorphism

Example:

#__author__ = 'valli_000'


class Animal(object):
    def __init__(self, Animalname):
        self.Animalname = Animalname

    def talk(self):
        pass


class Dog(Animal):
    def __init__(self, Animalname):
        super(self.__class__, self).__init__(Animalname)

    def talk(self):
        print "Sound of Animal " + self.Animalname + " is Bow Bow !"


class cat(Animal):
    def __init__(self, Animalname):
        super(self.__class__, self).__init__(Animalname)

    def talk(self):
        print "Sound of Animal " + self.Animalname + " is Meow Meow !"


x = cat("Pet 1")
x.talk()

y = Dog("Pet 2")
y.talk()


output:
C:\Python27\python.exe C:/Users/valli_000/PycharmProjects/Selflearning/Polymorphism.py

Sound of Animal Pet 1 is Meow Meow !
Sound of Animal Pet 2 is Bow Bow !

Process finished with exit code 0

Python Inheritace (accessing the init variables of base class from child class)


>>> class Baseclass(object):
...     def __init__(self, number):
...             self.number = number
...
...
>>> class childclass(Baseclass):

...     def __init__(self, number, number2):
...             self.number2 = number2
...             super(self.__class__, self).__init__(number)
...     def add(self):
...             return self.number + self.number2

>>> c = childclass(2,3)
>>> c.add()
5

Here, Accessing the init variables of base class from inside the child class  (i.e use the values of base class inside the child class)


Reference: http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods



Case 2:  (Here we are accessing the parent variables from the child object, not from the child class/methods)

class base:
...     var1 = "Base class"
...     def __init__(self, number):
...             self.number = 5
...

class child(base):
...     var2 = "child class"
...     def add(self):
...             pass



Output:

 y = child(3)
>>> y.number
5

Python: Method overriding

Python Method overriding:

Parent class:

class pclass:
...     var1="Im in parent class"
...     def add(self, number1, number2):
...             return number1 + number2


Child Class:


class cclass(pclass):
...     var1="Im in parent class"
...     def add(self, number1, number2, number3):

...             return number1 + number2+number3


Here, we override the "add" method of parent class in the child class. See the output


Output:

>> c = class()
>> c.add(1,2,3)
>> 6

So, it uses only the add method of the child class, not the parent class


Note: Child class is also called as sub class


Why overriding is required?

One reason for overriding parent's methods is because you may want special or different functionality in your subclass.