-> Encapsulate means to hide. Encapsulation is also called data hiding.You can think Encapsulation like a capsule (medicine tablet) which hides medicine inside it.
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation.
Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.
In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
A language mechanism for restricting access to some of the object's components.
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
=== PUBLIC, PROTECTED AND PRIVATE MEMBERS BEHAVIOR ===
>>> x = Encapsulation(11,13,17)
>>> x.public
11
>>> x._protected
13
>>> x._protected = 23
>>> x._protected
23
>>> x.__private
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Encapsulation' object has no attribute '__private‘
>>>
=== Inheritance in Python ===
The Python feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class.
The primary advantage of this feature is that you can add new methods to a class without modifying the existing class. It is called inheritance because the new class inherits all of the methods of the existing class. Extending this metaphor, the existing class is sometimes called the parent class. The new class may be called the child class or sometimes subclass.
===== SAMPLE CODE FOR BASIC INHERITANCE =====
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def show(self):
print 'Person Name: %s' %(self.name)
print 'Person Age: %s' %(self.age)
class Employe(Person):
def __init__(self,name,age,company,sal):
self.company=company
self.sal=sal
super(Employe,self).__init__(name,age)
def __repr__(self):
return str (self.name+self.age+self.company+self.sal)
def showme(self):
super(Employe,self).show()
print 'Company Name: %s'%(self.company)
print 'Employe Salary per Annum: %s' %(self.sal)
print '\n'
empdict={'guido':['45','PYTHON','500000'],'van':['25','JYTHON','200000'],'rossum':['35','ABC','400000']}
for key,value in empdict.iteritems():
print key,value
emp=Employe(key,value[0],value[1],value[2])
emp.showme()
=== POLYMORPHISM in Python ===
Polymorphism is something similar to a word having several different meanings depending on the context.
In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms")
is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically,
to allow an entity such as a variable, a function, or an object to have more than one form.
The classic example is the Shape class and all the classes that can inherit from it (square, circle, irregular polygon, splat and so on).
With polymorphism, each of these classes will have different underlying data. A point shape needs only two co-ordinates (assuming it's in a two-dimensional space of course). A circle needs a center and radius. A square or rectangle needs two co-ordinates for the top left and bottom right corners (and possibly) a rotation. An irregular polygon needs a series of lines.
=====SAMPLE CODE FOR POLYMORPHISM =====
class Person:
def __init__(self, name):
self.name = name
def designation(self):
raise NotImplementedError("Subclass must implement abstract method")
class Employe(Person):
def designation(self):
return 'Software Engineer‘
class Doctor(Person):
def designation(self):
return 'Cardiologist‘
class Student(Person):
def designation(self):
return 'Graduate Engineer'
persons = [Employe('Guido Van Rossum'),Doctor('Chalapathi'),Student('Robert')]
for person in persons:
print person.name + ': ' + person.designation()