Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Python Inheritance

Similar presentations


Presentation on theme: "OOP Python Inheritance"— Presentation transcript:

1 OOP Python Inheritance
The OOP Paradigm

2 Activity 1 Discuss: What is instantiation?
3 mins Discuss: What is instantiation? How can objects have different values from one another? – what allows this to happen?

3 Learning Habits Reasoning: Noticing details Adapting :
Thinking rigorously, methodically and giving explanations. Noticing details Adapting : Reflecting and making changes Empathising… …with feelings and views Effective use of time Questioning: Asking questions to get below the surface Listening… …to understand Imagining… …how things could be and seeing a range of possibilities Distilling… …what you have learnt and what you need to learn Collaboration: Working effectively with others Meta Learning: Talking about how you have been learning Imitation: Picking up good habits from others Independence: Working effectively alone Managing distractions… …and sustaining concentration Capitalising: Using resources purposefully Making links… …and recognising relevance Perseverance: Overcoming frustration and difficulty Planning… …your learning in advance

4 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Inheritance Understand Encapsulation and Private Attributes UML and Hierarchy Diagrams Introduction Last lesson we looked at instantiating objects - passing values to the object during instantiation. We also understood the importance of self. In this lesson we will look at inheritance

5 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Inheritance Understand Encapsulation and Private Attributes UML and Hierarchy Diagrams Inheritance Classes can inherit other classes. A class can inherit attributes and behaviour (methods) from other classes, called super-classes. A class which inherits from super-classes is called a Sub-class. Super-classes are sometimes called ancestors as well. There exists a hierarchy relationship between classes.

6 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Inheritance Understand Encapsulation and Private Attributes UML and Hierarchy Diagrams Inheritance …and this makes sense. If we had a class for a human we could create lots of ‘Human’ objects. But inefficiently, we would have to repeatedly set their genders to either ‘male’ or ‘female’ each time a ‘human’ object is created. So instead we could in fact ‘inherit’ this ‘Human’ class into a ‘Man’ class and a ‘Woman’ class. Both the ‘Man’ and ‘Woman’ classes will have the same attributes as a ‘human’ and same behaviours. However, we could set (for example) our ‘Man’s’ gender to ‘male’ so that if we need a ‘man’ object, we can use our ‘Man’ class which is set up with the correct data, instead of having to create a ‘Human’ object and customise it each time.

7 Object Orientated Programming
Learning Objectives: Understand the Object Orientated Programming paradigm Understand how to program using this paradigm in Python by: Understand how to create a class Understand how to write class attributes Understand how to write class methods Understand the importance of ‘Self’ Understand how to create an instance of a class (an object) Understand how to access object’s attributes and methods Understand how to instantiate a new object with unique/new data Understand how ‘Self’ works Inheritance Understand Encapsulation and Private Attributes UML and Hierarchy Diagrams Inheritance in Python Here we have a class called ‘Human’. If I wanted to created a class called ‘Man’ which shared the same attributes and methods as human, my ‘Man’ class can inherit the ‘Human’ class. We simply need two extra pieces of code to inherit another class: The parent class’s name A super constructor The parent class’ name is added to the child class’ parentheses (brackets). The parent class (superclass) constructor needs to be added inside the child class’ constructor.

8 Object Orientated Programming
Inheritance in Python The super constructor sets up the parent class inside the child class. In other words, the super constructor allows the ‘Man‘ class to have access to all of the ‘Human’ class’ attributes and methods. This is why, if we create an object from the ‘Man’ class, the object will have all attributes and methods from ‘Human’. The screen shot of the running program demonstrates this. Object created from ‘man’ Object now has attribute values as set in the ‘human’ class.

9 Object Orientated Programming
Accessing Parent Methods Now that the ‘Man’ class has inherited the parent class (Human) we can access the methods from the parent class in the same way as before. Speak method call

10 Object Orientated Programming
Overwriting Parent Attributes Obviously, creating a new class which simply inherits another is pointless. The reason for inheritance is usually because our new class needs to reuse certain attributes and methods but also overwrite some, with data and behaviours that are relevant to the new class and maybe even to add more functionality. In this ‘Man’ example, it makes sense to inherit ‘Human’, as ‘Man’ will have the same attributes, however, our ‘Man’ object will differ in that it’s gender will always be ‘Male’. So, in the super constructor, we can overwrite the human_gender attribute by passing this parameter in with the argument human_gender = “male”.

11 Object Orientated Programming
Adding “New Attributes” to the child class: In this example, you can see that we can easily add new attributes to our child class. Here, I have added a new attribute ‘hair_colour’ which receives a colour when a ‘Man’ object is created. Notice that the ‘Man’ class’ constructor has parameter ‘hair_colour’ – because this is not equal to a value, if no colour is passed in at instantiation, there is no default hair colour and so an error will occur.

12 Object Orientated Programming
Overwriting Parent Methods We can also overwrite parent methods by re-writing them in your new child class. Here you can see how the ‘Man’ class has a new version of the speak() method. So now, when the speak method is called on our ‘A_man’ object, it uses the ‘man’ class’ speak method.

13 Object Orientated Programming
Instantiation with new data (passing parameters into the constructor) from a child class Imagine we wanted to create a man object called ‘bob’ and we wanted to set his ‘name’, ‘age’, and nationality during instantiation… …this is what we could do! The following slides will show the movement of data as values are passed from class to class.

14 Object Orientated Programming
Instantiation with new data (passing parameters into the constructor) from a child class The arguments are passed into the sub class’ constructor in the order that they are presented.

15 Object Orientated Programming
Instantiation with new data (passing parameters into the constructor) from a child class These arguments are then passed into the super constructor. Notice that there is a 4th value (‘male’). This is so that each time a ‘man’ object created, it’s gender is hard coded into it.

16 Object Orientated Programming
Instantiation with new data (passing parameters into the constructor) from a child class These arguments are then passed into the parent class’ attributes.

17 Object Orientated Programming
Instantiation with new data (passing parameters into the constructor) from a child class So now, when the object’s data is accessed, all of the necessary attributes and methods accurately hold the data belonging to the ‘bob’ object.

18 Activity Create a parent class called ‘Fruit’
Give it the following attributes: Colour #set this to unknown Size #set this to unknown Taste #set this to unknown Give it the method print_description() which will print out a few lines describing itself in terms of colour, size and taste. Create a child class called “Tropical” Make it inherit ‘Fruit’ Make it overwrite the ‘Taste’ attribute so that it is set to ‘Sweet’ Create another child class called “Citrus” Make it overwrite the ‘Taste’ attribute so that it is set to ‘Bitter’. Give it an additional attribute called ‘Bitter Level’ Rewrite the print_description method so that it includes a description of the bitter level. Instantiate a ‘Mango’ object using the ‘Tropical’ class. Give the object the following values during instantiation: Red #colour Medium #size Call the print_description method and check it has the correct details. Instantiate a ‘Lemon’ object using the ‘Citrus’ class. Yellow #colour Small #size 8 #bitter level


Download ppt "OOP Python Inheritance"

Similar presentations


Ads by Google