The document discusses Object Oriented Programming in Python, focusing on the concept of inheritance and method overriding. It explains how to tailor classes for specific needs by inheriting properties from a base class and overriding methods in derived classes. Additionally, it includes an exercise to create a base class 'Animal' and its subclasses 'Mammal' and 'Bird'.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Lecture_6#_Inheritance
The document discusses Object Oriented Programming in Python, focusing on the concept of inheritance and method overriding. It explains how to tailor classes for specific needs by inheriting properties from a base class and overriding methods in derived classes. Additionally, it includes an exercise to create a base class 'Animal' and its subclasses 'Mammal' and 'Bird'.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
Object Oriented Programming
Using Python
Teemu Matilainen teemu.matilainen@savo nia.fi Lecture 6#
• You realize what inheritance is about
• You are able to inherit a class • You are able to override a method in a subclass What inheritance is about? How to inherit a class? Tailoring Classes for Specific Needs…
There are instances when you've already created a
class, but later find the need for unique characteristics in certain instances, rather than all. Alternatively, you might notice that you've defined two closely related classes with only slight distinctions. As programmers, our goal is to minimize repetition while ensuring clarity and readability. How can we address varying implementations of inherently similar objects in such cases? Example: If we want to change the email address, we must write two different functions… Inheritance: Using the inherited class change_email call BookContainer & BookShelf – Method overriding • In the Bookshelf class, there is a method named add_book. The base class BookContainer also defines a method with the same name. This phenomenon is known as method overriding. When a derived class has a method sharing the same name as the one in the base class, the version in the derived class takes precedence and replaces the original method when working with instances of the derived class.
• In the example, the concept is that when
adding a new book to a BookContainer, it is always placed at the top. However, with a Bookshelf, you have the flexibility to specify the location yourself. The list books method functions identically for both classes, as there is no overridden method in the derived class. BookContainer & BookShelf in action!!
The Bookshelf class also has access
to the list of books method. Through inheritance the method is a member of all the classes derived from the BookContainer class!!!! Inheritance and the scope of these properties A derived class inherits all properties from the base class. Properties are accessible from the derived class, unless they have been defined as private in the base class (with two underscores before the name of the properties).
As the attributes of a Bookshelf are
All the properties in the base class can be identical to a BookContainer, there accessed from the derived class with the function was no need to rewrite the super(). The self argument is left out from the constructor of Bookshelf. We simply method call, as Python adds it automatically. called the constructor of the base class: What if the attributes are not identical: can we still use the base class constructor in some way? Exercise: Inheritance • Create a Base Class: Animal • Create Subclasses: Mammal and Bird Exercise: Inheritance…