OOP Python Inheritance

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

OOP: Inheritance By: Lamiaa Said.
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
BLP Building Learning Power. The Four Rs Resourcefulness Reflectiveness Resilience Reciprocity.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
By Miss M Williams and Miss H Deacy. Excellent Schools: A vision for Schools in Wales in the 21 st century The development of learning skills, or what.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Alan Turing and Code Breaking
Activity 1 5 minutes to discuss and feedback on the following:
Activity 1 Input Device Output Device CPU RAM Hard Drive (Hard Disk)
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Object Oriented Programing (OOP)
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSCE 240 – Intro to Software Engineering Lecture 3.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Creating your own Handheld Games Console
Software Modelling Class Diagram. Class Diagrams The main building block in object oriented modeling They are used both for general conceptual modeling.
1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
JAVA By Waqas.
Learning Muscles.
Interface, Subclass, and Abstract Class Review
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
INTRODUCTION TO OOP Objective:
Lecture VI Objects The OOP Concept Defining Classes Methods
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Procedural Abstraction Object-Oriented Code
Object Oriented Analysis and Design
Object-oriented programming
Object Oriented Programming (OOP) LAB # 8
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Comp 249 Programming Methodology
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Advanced Programming Behnam Hatami Fall 2017.
Computer Programming with JAVA
Java – Inheritance.
CISC124 Assignment 4 on Inheritance due next Friday.
An Introduction to Object Orientated Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Thinking Ahead
Computational Thinking
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Planning longer answers:
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Object Oriented Programming in A Level
Review of Previous Lesson
OOP Python Instantiation & Understanding Self
Review of Previous Lesson
Thinking Abstractly
Object-Oriented PHP (1)
Classes and Methods 15-Aug-19.
Building Learning Power
Presentation transcript:

OOP Python Inheritance The OOP Paradigm

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?

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

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

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.

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.

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.

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.

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

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”.

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.

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.

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.

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.

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.

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.

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.

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