CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Inheritance Writing and using Classes effectively.
Classes and Inheritance. 2 As the building blocks of more complex systems, objects can be designed to interact with each other in one of three ways: Association:
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advanced Class 6. Topics Inheritance Class methods / static methods Class data vs. instance data.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Chapter 8 More Object Concepts
Week Inheritance and Polymorphism. Introduction Classes allow you to modify a program without really making changes to it. To elaborate, by subclassing.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Introduction to Python III CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
Introduction to Python III CIS 391: Artificial Intelligence Fall, 2008.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
PyGame - Unit 4 PyGame Unit – Object Oriented Programming OOP.
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.
CMSC202 Computer Science II for Majors Lecture 14 – Polymorphism Dr. Katherine Gibson.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson.
CMSC201 Computer Science I for Majors Lecture 20 – Classes and Modules Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Guide to Programming with Python
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Object Oriented Programming in Python: Defining Classes
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Today’s Objectives Review the important points of classes
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Lecture VI Objects The OOP Concept Defining Classes Methods
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Engineering Computing
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Creating and Deleting Instances Access to Attributes and Methods
Inheritance Basics Programming with Inheritance
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Encapsulation Inheritance PolyMorhpism
12. Classes & Objects a class factory makes objects + functions
Inheritance, Superclasses, Subclasses
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming in PHP
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Python 3 Some material adapted from Upenn cis391 slides and other sources.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Review of Previous Lesson
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Prof. Katherine Gibson Prof. Jeremy Dixon
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
CSE 231 Lab 13.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the book author, and previous iterations of the course

Last Class We Covered Constructors Difference between – Data attributes – Class attributes Special built-in methods and attributes Creating and using a class 2

Any Questions from Last Time?

Today’s Objectives To harness the power of inheritance To learn about subclasses and superclasses To be able to redefine a method To be able to extend a method – (Including __init__ ) 4

Find the Errors in the Code Below def student: def init(self, n, a, g): name = n age = a gpa = g def updateGPA(newGPA): gpa = newGPA def main(): val = new student("Alex", 21, 4.0) test = new student("Test", 18, 0) updateGPA(test, 3.26) main() 5 There are at least seven unique errors

Find the Errors in the Code Below def student: def init(self, n, a, g): name = n age = a gpa = g def updateGPA(newGPA): gpa = newGPA def main(): val = new student("Alex", 21, 4.0) test = new student("Test", 18, 0) updateGPA(test, 3.26) main() 6

Find the Errors in the Code Below class student: def __init__(self, name, age, gpa): self.name = name self.age = age self.gpa = gpa def updateGPA(self, newGPA): self.gpa = newGPA def main(): val = student("Alex", 21, 4.0) test = student("Test", 18, 0) test.updateGPA(3.26) main() 7

Inheritance

Inheritance Inheritance is when one class (the “child” class) is based upon another class (the “parent” class) The child class inherits most or all of its features from the parent class it is based on It is a very powerful tool available to you with Object-Oriented Programming 9

Inheritance Example For example: computer science students are a specific type of student They share attributes with every other student We can use inheritance to use those already defined attributes and methods of students for our computer science students 10

Inheritance Vocabulary The class that is inherited from is called the – Parent class – Ancestor – Superclass The class that does the inheriting is called a – Child class – Descendant – Subclass 11

Inheritance Code To create a child class, put the name of the parent class in parentheses when you initially define the class class cmscStudent(student): Now the child class cmscStudent has the properties and functions available to the parent class student 12

Extending a Class We may also say that the child class is extending the functionality of the parent class Child class inherits all of the methods and data attributes of the parent class – Also has its own methods and data attributes – We can even redefine parent methods! 13

Redefining Methods

Redefining Methods Redefining a method is when a child class implements its own version of that method To redefine a method, include a new method definition – with the same name as the parent class’s method – in the child class – Now child objects will use the new method 15

Redefining Example Here, we have an animal class as the parent and a dog class as the child class animal: # rest of class definition def speak(self): print("\"" + self.species + " noise\"") class dog(animal): def speak(self): print("Woof woof bark!") 16

Extending Methods Instead of completely overwriting a method, we can instead extend it for the child class When might we want to do this? – Constructor ( __init__ ) – Print function ( __repr__ ) – When else? 17

Extending a Method Want to execute both the original method in the parent class and some new code in the child class – To do this, explicitly call the parent’s version One major thing: you must pass in the self variable when you call a parent method – This is the only time you should do this! 18

Extending Example Now we have a cat class as the child, with an additional data attribute sleepsAllDay class animal: def __init__(self, name, species): self.name = name self.species = species class cat(animal): def __init__(self, name, sleepsAllDay): animal.__init__(self, name, "cat") self.sleepsAllDay = sleepsAllDay 19

Student Inheritance Example 20 class student: """A class representing a student.""" def __init__(self, name, age): self.full_name = name self.age = age def getAge(self): return self.age class cmscStudent (student): """A class extending student class to CMSC students.""" def __init__(self, name, age, section): # call __init__ for student student.__init__(self, name, age) self.section_num = section def getAge(self): # redefines getAge method entirely print ("Age: " + str(self.age))

21

Any Other Questions?

Announcements Lab has been cancelled this week! – Work on your project instead Project 1 is out – Due by Tuesday, November 17th at 8:59:59 PM – Do NOT procrastinate! Next Class: Recursion 23