SlideShare a Scribd company logo
VIII. INHERITANCE AND POLYMORPHISM

PYTHON PROGRAMMING

Engr. RANEL O. PADON
INHERITANCE
PYTHON PROGRAMMING TOPICS
I

•Introduction to Python Programming

II

•Python Basics

III

•Controlling the Program Flow

IV

•Program Components: Functions, Classes, Packages, and Modules

V

•Sequences (List and Tuples), and Dictionaries

VI

•Object-Based Programming: Classes and Objects

VII

•Customizing Classes and Operator Overloading

VIII

•Object-Oriented Programming: Inheritance and Polymorphism

IX

•Randomization Algorithms

X

•Exception Handling and Assertions

XI

•String Manipulation and Regular Expressions

XII

•File Handling and Processing

XIII

•GUI Programming Using Tkinter
INHERITANCE Background

Object-Based Programming
 programming using objects

Object-Oriented Programming
 programming using objects & hierarchies
INHERITANCE Background
Object-Oriented Programming
A family of classes is known as a class hierarchy.
As in a biological family, there are parent classes and child
classes.
INHERITANCE Background

Object-Oriented Programming
 the parent class is usually called base class or superclass
 the child class is known as a derived class or subclass
INHERITANCE Background
Object-Oriented Programming
Child classes can inherit data and methods from parent
classes, they can modify these data and methods, and they can
add their own data and methods.
The original class is still available and the separate child class is
small, since it does not need to repeat the code in the parent
class.
INHERITANCE Background

Object-Oriented Programming
The magic of object-oriented programming is that other parts of
the code do not need to distinguish whether an object is the
parent or the child – all generations in a family tree can be
treated as a unified object.
INHERITANCE Background

Object-Oriented Programming
In other words, one piece of code can work with all members
in a class family or hierarchy. This principle has revolutionized
the development of large computer systems.
INHERITANCE Reusing Attributes
INHERITANCE Reusing Attributes
INHERITANCE The Media Hierarchy
INHERITANCE Base and Derived Classes
INHERITANCE The CommunityMember Base Class
INHERITANCE The Shape Hierarchy
INHERITANCE Sample Implementation 1

Magulang

Anak
INHERITANCE Magulang Base Class

class Magulang:
def __init__(self, pangalan, kayamanan):
self.pangalan = pangalan
self.kayamanan = kayamanan
INHERITANCE Anak Derived Class

class Magulang:
def __init__(self, pangalan, kayamanan):
self.pangalan = pangalan
self.kayamanan = kayamanan

class Anak(Magulang):
def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000000):
Magulang.__init__(self, pangalan, kayamanan)
INHERITANCE Sample Execution
class Magulang:
def __init__(self, pangalan, kayamanan):
self.pangalan = pangalan
self.kayamanan = kayamanan

class Anak(Magulang):
def __init__(self, pangalan, kayamanan):
Magulang.__init__(self, pangalan, kayamanan)

gorio = Anak("Mang Gorio", 1000000)
print gorio.pangalan
print gorio.kayamanan
INHERITANCE Anak with Default Args

class Magulang:
def __init__(self, pangalan, kayamanan):
self.pangalan = pangalan
self.kayamanan = kayamanan
class Anak(Magulang):
def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000):
Magulang.__init__(self, pangalan, kayamanan)

print Anak().pangalan
print Anak().kayamanan
INHERITANCE Magulang with instance method
class Magulang:
def __init__(self, pangalan, kayamanan):
self.pangalan = pangalan
self.kayamanan = kayamanan

def getKayamanan(self):
return self.kayamanan
class Anak(Magulang):
def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000):
Magulang.__init__(self, pangalan, kayamanan)

print Anak().pangalan
print Anak().getKayamanan()
INHERITANCE Sample Implementation 2

Rectangle

Square
INHERITANCE Rectangle Base Class

class Rectangle():
def __init__(self, lapad, haba):
self.lapad = lapad
self.haba = haba
def getArea(self):
return self.lapad*self.haba
INHERITANCE Square Derived Class
class Rectangle():
def __init__(self, lapad, haba):
self.lapad = lapad
self.haba = haba
def getArea(self):
return self.lapad*self.haba

class Square(Rectangle):
def __init__(self, lapad):
Rectangle.__init__(self, lapad, lapad)
def getArea(self):
return self.lapad**2
INHERITANCE Sample Execution
class Rectangle():
def __init__(self, lapad, haba):
self.lapad = lapad
self.haba = haba

def getArea(self):
return self.lapad*self.haba
class Square(Rectangle):
def __init__(self, lapad):
Rectangle.__init__(self, lapad, lapad)
def getArea(self):
return self.lapad**2
print Square(3).getArea()
INHERITANCE Deadly Diamond of Death
What would be the version of bite() that will be used by Hybrid?
Dark Side
Forces
Villains
attack()

Vampire
Vampire
bite()

Werewolf
bite()

Hybrid
INHERITANCE Deadly Diamond of Death

Dark Side
Forces
Villains
attack()

Vampire
Vampire
bite()

Python allow a limited form of multiple
inheritance hence care must be
observed to avoid name collisions or
ambiguity.
Werewolf

bite()

Hybrid

Other languages, like Java, do not allow
multiple inheritance.
INHERITANCE Polymorphism
Polymorphism is a consequence of inheritance. It is concept
wherein a name may denote instances of many different classes as
long as they are related by some common superclass.
It is the ability of one object, to appear as and be used like another
object.
In real life, a male person could behave polymorphically:
he could be a teacher, father, husband, son, etc depending on the
context of the situation or the persons he is interacting with.
INHERITANCE Polymorphism
Polymorphism is also applicable to systems with same base/core
components or systems interacting via unified interfaces.
 USB plug and play devices
 “.exe” files

 Linux kernel as used in various distributions
(Ubuntu, Fedora, Mint, Arch)
 Linux filesystems (in Linux everything is a file)
The beauty of it is that you could make small code changes
but it could be utilized by all objects inheriting or
interacting with it.
INHERITANCE Polymorphism
INHERITANCE Polymorphism

Hugis

Tatsulok

Bilog
INHERITANCE Polymorphism

class Hugis():
def __init__(self, pangalan):
self.pangalan = pangalan
INHERITANCE Polymorphism
class Hugis():
def __init__(self, pangalan):
self.pangalan = pangalan
class Tatsulok(Hugis):
def __init__(self, pangalan, pundasyon, taas):
Hugis.__init__(self, pangalan)
self.pundasyon = pundasyon
self.taas = taas
def getArea(self):
return 0.5*self.pundasyon*self.taas
print Tatsulok("Tatsulok sa Talipapa", 3, 4).getArea()
INHERITANCE Polymorphism
class Hugis():
def __init__(self, pangalan):
self.pangalan = pangalan
class Bilog(Hugis):
def __init__(self, pangalan, radyus):
Hugis.__init__(self, pangalan)
self.radyus = radyus
def getArea(self):
return 3.1416*self.radyus**2
print Bilog("Bilugang Mundo", 2).getArea()
INHERITANCE Polymorphism
class Hugis():
def __init__(self, pangalan):

class Tatsulok(Hugis):
def __init__(self, pangalan, pundasyon, taas):
def getArea(self):
class Bilog(Hugis):
def __init__(self, pangalan, radyus):
def getArea(self):
koleksyonNgHugis = []
koleksyonNgHugis += [Tatsulok("Tatsulok sa Talipapa", 3, 4)]
koleksyonNgHugis += [Bilog("Bilugang Mundo", 2)]
for i in range(len(koleksyonNgHugis)):
print koleksyonNgHugis[i].getArea()

POLYMORPHISM
INHERITANCE Polymorphism
Polymorphism is not the same as method overloading or
method overriding.
Polymorphism is only concerned with the application of
specific implementations to an interface or a more generic
base class.
Method overloading refers to methods that have the same
name but different signatures inside the same class.
Method overriding is where a subclass replaces the
implementation of one or more of its parent's methods.
Neither method overloading nor method overriding are by
themselves implementations of polymorphism.
http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Polymorphism_in_object-oriented_programming.html
INHERITANCE Abstract Class
INHERITANCE Abstract Class

 there are cases in which we define classes but we don’t
want to create any objects
 they are merely used as a base or model class
 abstract base classes are too generic to define real objects,
that is, they are abstract or no physical manifestation (it’s
like the idea of knowing how a dog looks like but not
knowing how an ‘animal’ looks like, and in this case,
‘animal’ is an abstract concept)
INHERITANCE Abstract Class

 Abstract class is a powerful idea since you only have to
communicate with the parent (abstract class), and all
classes implementing that parent class will follow
accordingly. It’s also related to the Polymorphism.
INHERITANCE Abstract Class

 ‘Shape’ is an abstract class. We don’t know how a ‘shape’
looks like, but we know that at a minimum it must have a
dimension or descriptor (name, length, width, height, radius,
color, rotation, etc). And these descriptors could be reused
or extended by its children.
INHERITANCE Abstract Class

 Abstract classes lay down the foundation/framework on
which their children could further extend.
INHERITANCE Abstract Class
INHERITANCE The Gaming Metaphor

Gameplay

MgaTauhan
KamponNgKadiliman

Manananggal

HalimawSaBanga

…

Menu Options

…

Maps

Tagapagligtas

Panday

MangJose
INHERITANCE Abstract Class
Abstract Classes
MgaTauhan

KamponNgKadiliman

Manananggal

HalimawSaBanga

Tagapagligtas

Panday

MangJose
INHERITANCE Abstract Class
class MgaTauhan():
def __init__(self, pangalan, buhay):
if self.__class__ == MgaTauhan:
raise NotImplementedError, "abstract class po ito!"
self.pangalan = pangalan
self.buhay = buhay
def draw(self):
raise NotImplementedError, "kelangang i-draw ito"
INHERITANCE Abstract Class
class KamponNgKadiliman(MgaTauhan):
bilang = 0
def __init__(self, pangalan):
MgaTauhan.__init__(self, pangalan, 50)
if self.__class__ == KamponNgKadiliman:
raise NotImplementedError, "abstract class lang po ito!"
KamponNgKadiliman.bilang += 1

def attack(self):
return 5
def __del__(self):
KamponNgKadiliman.bilang -= 1
self.isOutNumbered()
def isOutNumbered(self):
if KamponNgKadiliman.bilang == 1:
print “umatras ang mga HungHang"
INHERITANCE Abstract Class
class Tagapagligtas(MgaTauhan):
bilang = 0
def __init__(self, pangalan):
MgaTauhan.__init__(self, pangalan, 100)
if self.__class__ == Tagapagligtas:
raise NotImplementedError, "abstract class lang po ito!"
Tagapagligtas.bilang += 1

def attack(self):
return 10
def __del__(self):
Tagapagligtas.bilang -= 1
self.isOutNumbered()
def isOutNumbered(self):
if Tagapagligtas.bilang == 1:
“umatras ang ating Tagapagligtas"
INHERITANCE Abstract Class
class Manananggal(KamponNgKadiliman):
def __init__(self, pangalan):
KamponNgKadiliman.__init__(self, pangalan)
def draw(self):
return "loading Mananaggal.jpg.."

def kapangyarihan(self):
return "mystical na dila"
INHERITANCE Abstract Class
class HalimawSaBanga(KamponNgKadiliman):
def __init__(self, pangalan):
KamponNgKadiliman.__init__(self, pangalan)
def draw(self):
return "loading HalimawSaBanga.jpg.."

def kapangyarihan(self):
return "kagat at heavy-armored na banga"
INHERITANCE Abstract Class
class Panday(Tagapagligtas):
def __init__(self, pangalan):
Tagapagligtas.__init__(self, pangalan)

def draw(self):
return "loading Panday.jpg.."
def kapangyarihan(self):
return "titanium na espada, galvanized pa!"
INHERITANCE Abstract Class
class MangJose(Tagapagligtas):
def __init__(self, pangalan):
Tagapagligtas.__init__(self, pangalan)
def draw(self):
return "loading MangJose.jpg.."

def kapangyarihan(self):
return "CD-R King gadgets"
INHERITANCE Abstract Class
creatures
creatures
creatures
creatures
creatures
creatures

= []
+= [Manananggal("Taga-Caloocan")]
+= [HalimawSaBanga("Taga-Siquijor")]
+= [MangJose("Taga-KrusNaLigas")]
+= [MangJose("Taga-Cotabato")]
+= [Panday("Taga-Cubao Ilalim")]

for i in range(len(creatures)):
print creatures[i].draw()

print KamponNgKadiliman.bilang
print Tagapagligtas.bilang
del creatures[0]

loading Manananggal.jpg..
loading HalimawSaBanga.jpg..
loading MangJose.jpg..
loading MangJose.jpg..
loading Panday.jpg..
2
3
atras ang mga Hunghang!

This simple line is so powerful,
since it makes the current Creature
draw itself regardless of what
Creature it is!
INHERITANCE Flexibility and Power

Re-use, Improve, Extend
OOP: INHERITANCE is Powerful!
REFERENCES

 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/information used here have no proper source citation, and I do
not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse
and reintegrate materials that I think are useful or cool, then present them in another light,
form, or perspective. Moreover, the images/information here are mainly used for
illustration/educational purposes only, in the spirit of openness of data, spreading light, and
empowering people with knowledge. 

More Related Content

What's hot (20)

Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
Mutinda Boniface
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
Abdul Rahman Masri Attal
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
tuan vo
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Oops Concept Java
Oops Concept JavaOops Concept Java
Oops Concept Java
Kamlesh Singh
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Oops ppt
Oops pptOops ppt
Oops ppt
abhayjuneja
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
Md. Tanvir Hossain
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 
Oop java
Oop javaOop java
Oop java
Minal Maniar
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
Rai Saheb Bhanwar Singh College Nasrullaganj
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
baabtra.com - No. 1 supplier of quality freshers
 
Beginning OOP in PHP
Beginning OOP in PHPBeginning OOP in PHP
Beginning OOP in PHP
David Stockton
 
CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)
Rubén Izquierdo Beviá
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
tuan vo
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
Olivier Bacs
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
Md. Tanvir Hossain
 
CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)CLTL python course: Object Oriented Programming (1/3)
CLTL python course: Object Oriented Programming (1/3)
Rubén Izquierdo Beviá
 

Viewers also liked (11)

Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
Ranel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Python Programming - XII. File Processing
Python Programming - XII. File ProcessingPython Programming - XII. File Processing
Python Programming - XII. File Processing
Ranel Padon
 
Python Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator OverloadingPython Programming - VII. Customizing Classes and Operator Overloading
Python Programming - VII. Customizing Classes and Operator Overloading
Ranel Padon
 
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Ranel Padon
 
Switchable Map APIs with Drupal
Switchable Map APIs with DrupalSwitchable Map APIs with Drupal
Switchable Map APIs with Drupal
Ranel Padon
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
Ranel Padon
 
Python Programming - XIII. GUI Programming
Python Programming - XIII. GUI ProgrammingPython Programming - XIII. GUI Programming
Python Programming - XIII. GUI Programming
Ranel Padon
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel Padon
 
Python Programming - IX. On Randomness
Python Programming - IX. On RandomnessPython Programming - IX. On Randomness
Python Programming - IX. On Randomness
Ranel Padon
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Programming - I. Introduction
Python Programming - I. IntroductionPython Programming - I. Introduction
Python Programming - I. Introduction
Ranel Padon
 
Ad

Similar to Python Programming - VIII. Inheritance and Polymorphism (20)

Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Inheritance
InheritanceInheritance
Inheritance
JayanthiNeelampalli
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
All about python Inheritance.python codingdf
All about python Inheritance.python codingdfAll about python Inheritance.python codingdf
All about python Inheritance.python codingdf
adipapai181023
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
inheritance in python with full detail.ppt
inheritance in python with full detail.pptinheritance in python with full detail.ppt
inheritance in python with full detail.ppt
ssuser7b0a4d
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
Datacademy.ai
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Inheritance Super and MRO _
Inheritance Super and MRO               _Inheritance Super and MRO               _
Inheritance Super and MRO _
swati463221
 
OOP-part-2 object oriented programming.pptx
OOP-part-2 object oriented programming.pptxOOP-part-2 object oriented programming.pptx
OOP-part-2 object oriented programming.pptx
palmakyonna
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Object-Oriented Programming (OO) in pythonP) in python.pptx
Object-Oriented Programming (OO)  in pythonP)  in python.pptxObject-Oriented Programming (OO)  in pythonP)  in python.pptx
Object-Oriented Programming (OO) in pythonP) in python.pptx
institute of Geoinformatics and Earth Observation at PMAS ARID Agriculture University of Rawalpindi
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
arthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instantarthimetic operator,classes,objects,instant
arthimetic operator,classes,objects,instant
ssuser77162c
 
All about python Inheritance.python codingdf
All about python Inheritance.python codingdfAll about python Inheritance.python codingdf
All about python Inheritance.python codingdf
adipapai181023
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
rohitsharma24121
 
inheritance in python with full detail.ppt
inheritance in python with full detail.pptinheritance in python with full detail.ppt
inheritance in python with full detail.ppt
ssuser7b0a4d
 
Inheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python ProgrammingInheritance_in_OOP_using Python Programming
Inheritance_in_OOP_using Python Programming
abigailjudith8
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Class and Objects in python programming.pptx
Class and Objects in python programming.pptxClass and Objects in python programming.pptx
Class and Objects in python programming.pptx
Rajtherock
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
Datacademy.ai
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Inheritance Super and MRO _
Inheritance Super and MRO               _Inheritance Super and MRO               _
Inheritance Super and MRO _
swati463221
 
OOP-part-2 object oriented programming.pptx
OOP-part-2 object oriented programming.pptxOOP-part-2 object oriented programming.pptx
OOP-part-2 object oriented programming.pptx
palmakyonna
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Ad

More from Ranel Padon (9)

The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
Ranel Padon
 
CKEditor Widgets with Drupal
CKEditor Widgets with DrupalCKEditor Widgets with Drupal
CKEditor Widgets with Drupal
Ranel Padon
 
Views Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views ModuleViews Unlimited: Unleashing the Power of Drupal's Views Module
Views Unlimited: Unleashing the Power of Drupal's Views Module
Ranel Padon
 
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Batch Scripting with Drupal (Featuring the EntityFieldQuery API)
Ranel Padon
 
PyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputationPyCon PH 2014 - GeoComputation
PyCon PH 2014 - GeoComputation
Ranel Padon
 
Power and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQueryPower and Elegance - Leaflet + jQuery
Power and Elegance - Leaflet + jQuery
Ranel Padon
 
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
Ranel Padon
 
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)Of Nodes and Maps (Web Mapping with Drupal - Part II)
Of Nodes and Maps (Web Mapping with Drupal - Part II)
Ranel Padon
 
Web Mapping with Drupal
Web Mapping with DrupalWeb Mapping with Drupal
Web Mapping with Drupal
Ranel Padon
 

Recently uploaded (20)

LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 

Python Programming - VIII. Inheritance and Polymorphism

  • 1. VIII. INHERITANCE AND POLYMORPHISM PYTHON PROGRAMMING Engr. RANEL O. PADON
  • 3. PYTHON PROGRAMMING TOPICS I •Introduction to Python Programming II •Python Basics III •Controlling the Program Flow IV •Program Components: Functions, Classes, Packages, and Modules V •Sequences (List and Tuples), and Dictionaries VI •Object-Based Programming: Classes and Objects VII •Customizing Classes and Operator Overloading VIII •Object-Oriented Programming: Inheritance and Polymorphism IX •Randomization Algorithms X •Exception Handling and Assertions XI •String Manipulation and Regular Expressions XII •File Handling and Processing XIII •GUI Programming Using Tkinter
  • 4. INHERITANCE Background Object-Based Programming  programming using objects Object-Oriented Programming  programming using objects & hierarchies
  • 5. INHERITANCE Background Object-Oriented Programming A family of classes is known as a class hierarchy. As in a biological family, there are parent classes and child classes.
  • 6. INHERITANCE Background Object-Oriented Programming  the parent class is usually called base class or superclass  the child class is known as a derived class or subclass
  • 7. INHERITANCE Background Object-Oriented Programming Child classes can inherit data and methods from parent classes, they can modify these data and methods, and they can add their own data and methods. The original class is still available and the separate child class is small, since it does not need to repeat the code in the parent class.
  • 8. INHERITANCE Background Object-Oriented Programming The magic of object-oriented programming is that other parts of the code do not need to distinguish whether an object is the parent or the child – all generations in a family tree can be treated as a unified object.
  • 9. INHERITANCE Background Object-Oriented Programming In other words, one piece of code can work with all members in a class family or hierarchy. This principle has revolutionized the development of large computer systems.
  • 13. INHERITANCE Base and Derived Classes
  • 17. INHERITANCE Magulang Base Class class Magulang: def __init__(self, pangalan, kayamanan): self.pangalan = pangalan self.kayamanan = kayamanan
  • 18. INHERITANCE Anak Derived Class class Magulang: def __init__(self, pangalan, kayamanan): self.pangalan = pangalan self.kayamanan = kayamanan class Anak(Magulang): def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000000): Magulang.__init__(self, pangalan, kayamanan)
  • 19. INHERITANCE Sample Execution class Magulang: def __init__(self, pangalan, kayamanan): self.pangalan = pangalan self.kayamanan = kayamanan class Anak(Magulang): def __init__(self, pangalan, kayamanan): Magulang.__init__(self, pangalan, kayamanan) gorio = Anak("Mang Gorio", 1000000) print gorio.pangalan print gorio.kayamanan
  • 20. INHERITANCE Anak with Default Args class Magulang: def __init__(self, pangalan, kayamanan): self.pangalan = pangalan self.kayamanan = kayamanan class Anak(Magulang): def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000): Magulang.__init__(self, pangalan, kayamanan) print Anak().pangalan print Anak().kayamanan
  • 21. INHERITANCE Magulang with instance method class Magulang: def __init__(self, pangalan, kayamanan): self.pangalan = pangalan self.kayamanan = kayamanan def getKayamanan(self): return self.kayamanan class Anak(Magulang): def __init__(self, pangalan = "Juan Tamad", kayamanan = 1000): Magulang.__init__(self, pangalan, kayamanan) print Anak().pangalan print Anak().getKayamanan()
  • 22. INHERITANCE Sample Implementation 2 Rectangle Square
  • 23. INHERITANCE Rectangle Base Class class Rectangle(): def __init__(self, lapad, haba): self.lapad = lapad self.haba = haba def getArea(self): return self.lapad*self.haba
  • 24. INHERITANCE Square Derived Class class Rectangle(): def __init__(self, lapad, haba): self.lapad = lapad self.haba = haba def getArea(self): return self.lapad*self.haba class Square(Rectangle): def __init__(self, lapad): Rectangle.__init__(self, lapad, lapad) def getArea(self): return self.lapad**2
  • 25. INHERITANCE Sample Execution class Rectangle(): def __init__(self, lapad, haba): self.lapad = lapad self.haba = haba def getArea(self): return self.lapad*self.haba class Square(Rectangle): def __init__(self, lapad): Rectangle.__init__(self, lapad, lapad) def getArea(self): return self.lapad**2 print Square(3).getArea()
  • 26. INHERITANCE Deadly Diamond of Death What would be the version of bite() that will be used by Hybrid? Dark Side Forces Villains attack() Vampire Vampire bite() Werewolf bite() Hybrid
  • 27. INHERITANCE Deadly Diamond of Death Dark Side Forces Villains attack() Vampire Vampire bite() Python allow a limited form of multiple inheritance hence care must be observed to avoid name collisions or ambiguity. Werewolf bite() Hybrid Other languages, like Java, do not allow multiple inheritance.
  • 28. INHERITANCE Polymorphism Polymorphism is a consequence of inheritance. It is concept wherein a name may denote instances of many different classes as long as they are related by some common superclass. It is the ability of one object, to appear as and be used like another object. In real life, a male person could behave polymorphically: he could be a teacher, father, husband, son, etc depending on the context of the situation or the persons he is interacting with.
  • 29. INHERITANCE Polymorphism Polymorphism is also applicable to systems with same base/core components or systems interacting via unified interfaces.  USB plug and play devices  “.exe” files  Linux kernel as used in various distributions (Ubuntu, Fedora, Mint, Arch)  Linux filesystems (in Linux everything is a file) The beauty of it is that you could make small code changes but it could be utilized by all objects inheriting or interacting with it.
  • 32. INHERITANCE Polymorphism class Hugis(): def __init__(self, pangalan): self.pangalan = pangalan
  • 33. INHERITANCE Polymorphism class Hugis(): def __init__(self, pangalan): self.pangalan = pangalan class Tatsulok(Hugis): def __init__(self, pangalan, pundasyon, taas): Hugis.__init__(self, pangalan) self.pundasyon = pundasyon self.taas = taas def getArea(self): return 0.5*self.pundasyon*self.taas print Tatsulok("Tatsulok sa Talipapa", 3, 4).getArea()
  • 34. INHERITANCE Polymorphism class Hugis(): def __init__(self, pangalan): self.pangalan = pangalan class Bilog(Hugis): def __init__(self, pangalan, radyus): Hugis.__init__(self, pangalan) self.radyus = radyus def getArea(self): return 3.1416*self.radyus**2 print Bilog("Bilugang Mundo", 2).getArea()
  • 35. INHERITANCE Polymorphism class Hugis(): def __init__(self, pangalan): class Tatsulok(Hugis): def __init__(self, pangalan, pundasyon, taas): def getArea(self): class Bilog(Hugis): def __init__(self, pangalan, radyus): def getArea(self): koleksyonNgHugis = [] koleksyonNgHugis += [Tatsulok("Tatsulok sa Talipapa", 3, 4)] koleksyonNgHugis += [Bilog("Bilugang Mundo", 2)] for i in range(len(koleksyonNgHugis)): print koleksyonNgHugis[i].getArea() POLYMORPHISM
  • 36. INHERITANCE Polymorphism Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism. http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Polymorphism_in_object-oriented_programming.html
  • 38. INHERITANCE Abstract Class  there are cases in which we define classes but we don’t want to create any objects  they are merely used as a base or model class  abstract base classes are too generic to define real objects, that is, they are abstract or no physical manifestation (it’s like the idea of knowing how a dog looks like but not knowing how an ‘animal’ looks like, and in this case, ‘animal’ is an abstract concept)
  • 39. INHERITANCE Abstract Class  Abstract class is a powerful idea since you only have to communicate with the parent (abstract class), and all classes implementing that parent class will follow accordingly. It’s also related to the Polymorphism.
  • 40. INHERITANCE Abstract Class  ‘Shape’ is an abstract class. We don’t know how a ‘shape’ looks like, but we know that at a minimum it must have a dimension or descriptor (name, length, width, height, radius, color, rotation, etc). And these descriptors could be reused or extended by its children.
  • 41. INHERITANCE Abstract Class  Abstract classes lay down the foundation/framework on which their children could further extend.
  • 43. INHERITANCE The Gaming Metaphor Gameplay MgaTauhan KamponNgKadiliman Manananggal HalimawSaBanga … Menu Options … Maps Tagapagligtas Panday MangJose
  • 44. INHERITANCE Abstract Class Abstract Classes MgaTauhan KamponNgKadiliman Manananggal HalimawSaBanga Tagapagligtas Panday MangJose
  • 45. INHERITANCE Abstract Class class MgaTauhan(): def __init__(self, pangalan, buhay): if self.__class__ == MgaTauhan: raise NotImplementedError, "abstract class po ito!" self.pangalan = pangalan self.buhay = buhay def draw(self): raise NotImplementedError, "kelangang i-draw ito"
  • 46. INHERITANCE Abstract Class class KamponNgKadiliman(MgaTauhan): bilang = 0 def __init__(self, pangalan): MgaTauhan.__init__(self, pangalan, 50) if self.__class__ == KamponNgKadiliman: raise NotImplementedError, "abstract class lang po ito!" KamponNgKadiliman.bilang += 1 def attack(self): return 5 def __del__(self): KamponNgKadiliman.bilang -= 1 self.isOutNumbered() def isOutNumbered(self): if KamponNgKadiliman.bilang == 1: print “umatras ang mga HungHang"
  • 47. INHERITANCE Abstract Class class Tagapagligtas(MgaTauhan): bilang = 0 def __init__(self, pangalan): MgaTauhan.__init__(self, pangalan, 100) if self.__class__ == Tagapagligtas: raise NotImplementedError, "abstract class lang po ito!" Tagapagligtas.bilang += 1 def attack(self): return 10 def __del__(self): Tagapagligtas.bilang -= 1 self.isOutNumbered() def isOutNumbered(self): if Tagapagligtas.bilang == 1: “umatras ang ating Tagapagligtas"
  • 48. INHERITANCE Abstract Class class Manananggal(KamponNgKadiliman): def __init__(self, pangalan): KamponNgKadiliman.__init__(self, pangalan) def draw(self): return "loading Mananaggal.jpg.." def kapangyarihan(self): return "mystical na dila"
  • 49. INHERITANCE Abstract Class class HalimawSaBanga(KamponNgKadiliman): def __init__(self, pangalan): KamponNgKadiliman.__init__(self, pangalan) def draw(self): return "loading HalimawSaBanga.jpg.." def kapangyarihan(self): return "kagat at heavy-armored na banga"
  • 50. INHERITANCE Abstract Class class Panday(Tagapagligtas): def __init__(self, pangalan): Tagapagligtas.__init__(self, pangalan) def draw(self): return "loading Panday.jpg.." def kapangyarihan(self): return "titanium na espada, galvanized pa!"
  • 51. INHERITANCE Abstract Class class MangJose(Tagapagligtas): def __init__(self, pangalan): Tagapagligtas.__init__(self, pangalan) def draw(self): return "loading MangJose.jpg.." def kapangyarihan(self): return "CD-R King gadgets"
  • 52. INHERITANCE Abstract Class creatures creatures creatures creatures creatures creatures = [] += [Manananggal("Taga-Caloocan")] += [HalimawSaBanga("Taga-Siquijor")] += [MangJose("Taga-KrusNaLigas")] += [MangJose("Taga-Cotabato")] += [Panday("Taga-Cubao Ilalim")] for i in range(len(creatures)): print creatures[i].draw() print KamponNgKadiliman.bilang print Tagapagligtas.bilang del creatures[0] loading Manananggal.jpg.. loading HalimawSaBanga.jpg.. loading MangJose.jpg.. loading MangJose.jpg.. loading Panday.jpg.. 2 3 atras ang mga Hunghang! This simple line is so powerful, since it makes the current Creature draw itself regardless of what Creature it is!
  • 53. INHERITANCE Flexibility and Power Re-use, Improve, Extend
  • 54. OOP: INHERITANCE is Powerful!
  • 55. REFERENCES  Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).  Disclaimer: Most of the images/information used here have no proper source citation, and I do not claim ownership of these either. I don’t want to reinvent the wheel, and I just want to reuse and reintegrate materials that I think are useful or cool, then present them in another light, form, or perspective. Moreover, the images/information here are mainly used for illustration/educational purposes only, in the spirit of openness of data, spreading light, and empowering people with knowledge. 