SlideShare a Scribd company logo
Class inheritance 13 session - SHAN
Class Inheritance
Overriding Methods
Data Hiding
Class Inheritance
✔ Create a class by deriving it from a preexisting class by listing the
parent class in parentheses after the new class name.
✔ The child class inherits the attributes of its parent class, and you can
use those attributes as if they were defined in the child class. A child
class can also override data members and methods from the parent.
SYNTAX
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string'
class_suite
class Parent: # define parent class
parentAttr = 100
def __init__(self):
print "Calling parent constructor"
def parentMethod(self):
print 'Calling parent method'
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print "Parent attribute :", Parent.parentAttr
class Child(Parent): # define child class
def childMethod(self):
print 'Calling child method'
c = Child() # instance of child
c.childMethod() # child calls its method
c.parentMethod() # calls parent's method
c.setAttr(200) # again call parent's method
c.getAttr() # again call parent's method
OUTPUT:
Calling child constructor
Calling child method
Calling parent method
class A: # define your class A
.....
class B: # define your calss B
.....
class C(A, B): # subclass of A and B
.....
✔ The issubclass(sub, sup) boolean function returns true if the given
subclass sub is indeed a subclass of the superclass sup.
✔ The isinstance(obj, Class) boolean function returns true if obj is an
instance of class Class or is an instance of a subclass of Class
Overriding Methods
✔ One reason for overriding parent's methods is because you may want
special or different functionality in your subclass.
class Parent: # define parent class
def myMethod(self):
print 'Calling parent method'
class Child(Parent): # define child class
def myMethod(self):
print 'Calling child method'
c = Child() # instance of child
c.myMethod() # child calls overridden method
Data Hiding
An object's attributes may or may not be visible outside the class
definition. For these cases, you can name attributes with a double
underscore prefix, and those attributes will not be directly visible to
outsiders
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount
OUTPUT:
1
2
Traceback (most recent call last):
File "test.py", line 12, in <module>
print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'
Python protects those members by internally changing the name to
include the class name. You can access such attributes as
object._className__attrName.
print counter._JustCounter__secretCount
OUTPUT:
1
2
2
Class inheritance 13 session - SHAN

More Related Content

PPT
DBIC 3 - Primer
KEY
What's new in Django 1.2?
PDF
Et si on en finissait avec CRUD ?
PPT
Perl Intro 7 Subroutines
PDF
Some Examples in R.
PPTX
PHP string-part 1
PPT
Python session 11
PPT
V irtualisation.1
DBIC 3 - Primer
What's new in Django 1.2?
Et si on en finissait avec CRUD ?
Perl Intro 7 Subroutines
Some Examples in R.
PHP string-part 1
Python session 11
V irtualisation.1

Viewers also liked (9)

PPT
Python session.11 By Shanmugam
PPT
Python session 12
PDF
Regex, Python & Twitter
PPT
PPTX
Java Generics Adoption: How New Features are Introduced Championed or Ignored
PPTX
Python advanced 2. regular expression in python
ODP
Exception Handling in Perl
PPTX
Handling error & exception in php
PDF
Lead Generation on SlideShare: A How-to Guide
Python session.11 By Shanmugam
Python session 12
Regex, Python & Twitter
Java Generics Adoption: How New Features are Introduced Championed or Ignored
Python advanced 2. regular expression in python
Exception Handling in Perl
Handling error & exception in php
Lead Generation on SlideShare: A How-to Guide
Ad

Similar to Class inheritance 13 session - SHAN (20)

PDF
Python programming : Inheritance and polymorphism
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPT
Python session 7 by Shan
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PPTX
Python_Object_Oriented_Programming.pptx
PPT
Lecture topic - Python class lecture.ppt
PPT
Lecture on Python class -lecture123456.ppt
DOCX
Python Metaclasses
PPT
Python3
PDF
Python programming : Classes objects
PPT
Introduction to Python - Part Three
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
PDF
Object_Oriented_Programming_Unit3.pdf
PPT
Php object orientation and classes
PPT
Spsl v unit - final
PPT
Spsl vi unit final
PPTX
Python advance
PPTX
Unit – V Object Oriented Programming in Python.pptx
Python programming : Inheritance and polymorphism
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Python session 7 by Shan
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Python_Object_Oriented_Programming.pptx
Lecture topic - Python class lecture.ppt
Lecture on Python class -lecture123456.ppt
Python Metaclasses
Python3
Python programming : Classes objects
Introduction to Python - Part Three
Basic_concepts_of_OOPS_in_Python.pptx
Object_Oriented_Programming_Unit3.pdf
Php object orientation and classes
Spsl v unit - final
Spsl vi unit final
Python advance
Unit – V Object Oriented Programming in Python.pptx
Ad

More from Navaneethan Naveen (20)

PPT
Virtualisation-11
PPT
Networking session-4-final by aravind.R
PPT
Networking session3
PPT
WIN-ADCS-10
PPT
Python session 10
PPT
Python multithreading session 9 - shanmugam
PPT
Python session 8
PPT
PPT
Virtualization session 8
PDF
Virtualization session 7 by Gugan
PPT
Virtualization s4.1
PPT
Python session 6
PPT
Gpo windows(4)
PPTX
Windows session 5 : Basics of active directory
PPT
Network session 1 OSI Model
PPT
Http request&response by Vignesh 15 MAR 2014
PPT
Virtualization s4
PPT
PPT
Lavanya demo
PPT
Windows session 4- by Giri
Virtualisation-11
Networking session-4-final by aravind.R
Networking session3
WIN-ADCS-10
Python session 10
Python multithreading session 9 - shanmugam
Python session 8
Virtualization session 8
Virtualization session 7 by Gugan
Virtualization s4.1
Python session 6
Gpo windows(4)
Windows session 5 : Basics of active directory
Network session 1 OSI Model
Http request&response by Vignesh 15 MAR 2014
Virtualization s4
Lavanya demo
Windows session 4- by Giri

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Insiders guide to clinical Medicine.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
TR - Agricultural Crops Production NC III.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Insiders guide to clinical Medicine.pdf
RMMM.pdf make it easy to upload and study
Final Presentation General Medicine 03-08-2024.pptx
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Class inheritance 13 session - SHAN

  • 3. Class Inheritance ✔ Create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name. ✔ The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent. SYNTAX class SubClassName (ParentClass1[, ParentClass2, ...]): 'Optional class documentation string' class_suite
  • 4. class Parent: # define parent class parentAttr = 100 def __init__(self): print "Calling parent constructor" def parentMethod(self): print 'Calling parent method' def setAttr(self, attr): Parent.parentAttr = attr def getAttr(self): print "Parent attribute :", Parent.parentAttr class Child(Parent): # define child class
  • 5. def childMethod(self): print 'Calling child method' c = Child() # instance of child c.childMethod() # child calls its method c.parentMethod() # calls parent's method c.setAttr(200) # again call parent's method c.getAttr() # again call parent's method OUTPUT: Calling child constructor Calling child method Calling parent method
  • 6. class A: # define your class A ..... class B: # define your calss B ..... class C(A, B): # subclass of A and B ..... ✔ The issubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the superclass sup. ✔ The isinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance of a subclass of Class
  • 7. Overriding Methods ✔ One reason for overriding parent's methods is because you may want special or different functionality in your subclass. class Parent: # define parent class def myMethod(self): print 'Calling parent method' class Child(Parent): # define child class def myMethod(self): print 'Calling child method' c = Child() # instance of child c.myMethod() # child calls overridden method
  • 8. Data Hiding An object's attributes may or may not be visible outside the class definition. For these cases, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders class JustCounter: __secretCount = 0 def count(self): self.__secretCount += 1 print self.__secretCount counter = JustCounter() counter.count() counter.count() print counter.__secretCount
  • 9. OUTPUT: 1 2 Traceback (most recent call last): File "test.py", line 12, in <module> print counter.__secretCount AttributeError: JustCounter instance has no attribute '__secretCount'
  • 10. Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName. print counter._JustCounter__secretCount OUTPUT: 1 2 2