INHERITANCE AND INTERFACES 7

Similar documents
INHERITANCE AND NONLOCAL 6

OBJECT ORIENTED PROGRAMMING 5

Spring 2018 Discussion 5: February 28, Object Oriented Programming

OBJECT ORIENTED PROGRAMMING 7

OBJECT ORIENTED PROGRAMMING 7

OBJECT ORIENTED PROGRAMMING 6

CS 11 python track: lecture 4

Python for Finance. Advanced Features. Andras Niedermayer

CS1 Lecture 27 Mar. 26, 2018

OBJECT ORIENTED PROGRAMMING

6. Object-Oriented Programming, II. Programming and Algorithms II Degree in Bioinformatics Fall 2018

CMSC201 Computer Science I for Majors

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

SCHEME INTERPRETER GUIDE 4

Lecture 38: Python. CS 51G Spring 2018 Kim Bruce

Review 2. Classes and Subclasses

Lecture 18. Classes and Types

Fundamentals of Programming. October 15, 2017

Part V. Object-oriented Programming. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

CME 193: Introduction to Scientific Python Lecture 4: File I/O and Classes

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

APT Session 2: Python

PREPARING FOR PRELIM 2

Exceptions. raise type(message) raise Exception(message)

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 9.

Lecture 21. Programming with Subclasses

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Creating a new data type

Object Oriented Programming

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Making things move. Time and Space

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 14 Object Oriented Programming TODAY 7/11/2012 WHERE WE WERE: FUNCTIONAL PROGRAMMING

Python An Introduction

Iterators & Generators

Object-oriented programming in Python (2)

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 1301 Post Exam 3 Practice Spring 2016

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Overloading. F21SC Industrial Programming: Python: Advanced Language Features. Overloading. Overloading arithmetic operations

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Sorting. Sorting. Selection sort

Python Boot Camp. Day 3

Unit E Step-by-Step: Programming with Python

Object Oriented Programming #10

Jython. secondary. memory

Overview. Elements of Programming Languages. Objects. Self-Reference

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

COMP 250 Fall inheritance Nov. 17, 2017

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

EXCEPTIONS, CALCULATOR 10

CS 1110 Prelim 2 November 6th, 2012

CSC148: Week 2

MITOCW watch?v=flgjisf3l78

CS 1110 Final Exam, May 2018

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

INTERPRETERS AND TAIL CALLS 9

ENGR 101 Engineering Design Workshop

Week 2. Classes and Objects

Using Inheritance to Share Implementations

UNIVERSITETET I OSLO

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

EXAM PREPARATION SECTION 1

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Object-Oriented Programming More Inheritance

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

Object Orientated Programming Details COMP360

Week 11: Class Design

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018

PREPARING FOR THE FINAL EXAM

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Objects CHAPTER 6. FIGURE 1. Concrete syntax for the P 3 subset of Python. (In addition to that of P 2.)

Lecture 20. Subclasses & Inheritance

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

The Science of Computing II Living with Cyber object-oriented paradigm State and behavior...the basic idea

CS 1301 Exam 1 Fall 2014

Quiz. a 1 a 2 a 3 a 4. v 1 v 2 v 3. Let a 1 = [1, 0, 1], a 2 = [2, 1, 0], a 3 = [10, 1, 2], a 4 = [0, 0, 1]. Compute the row-matrix-by-vector product

Refreshing last time

Class extension and. Exception handling. Genome 559

CS 1301 Exam 1 Fall 2014

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

T H E I N T E R A C T I V E S H E L L

CS 1110: Introduction to Computing Using Python Loop Invariants

Review 3. Exceptions and Try-Except Blocks

CS 1110 Final Exam Solutions May 2018

TUPLES AND RECURSIVE LISTS 5

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

The Pyth Language. Administrivia

Inheritance (Outsource: )

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

15: Polymorphism & Virtual Functions

Python for Non-programmers

x = e Python tries to avoid overwrites i Python tries to avoid overwrites next Monday Programming Assignment #7 on Prolog quarter Assignment Revisited

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

Transcription:

INHERITANCE AND INTERFACES 7 COMPUTER SCIENCE 61A October 16, 2014 1 Inheritance Today, we explore another powerful tool that comes with object-oriented programming inheritance. Suppose we want to write Dog and Cat classes. Here s our first attempt: class Dog(object): def init (self, name, owner, color): self.name = name self.owner = owner self.color = color def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") return self.name + " says woof!" class Cat(object): def init (self, name, owner, lives=9): self.name = name self.owner = owner self.lives = lives def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") return self.name + " says meow!" 1

DISCUSSION 7: INHERITANCE AND INTERFACES Page 2 Notice that both the Dog and Cat classes have a name, owner, eat method, and talk method. That s a lot of effort for so much repeated code! This is where inheritance comes in. In Python, a class can inherit the instance variables and methods of a another class without having to type them all out again. For example: class Foo(object): # This is the superclass class Bar(Foo): # This is the subclass Bar inherits from Foo. We call Foo the superclass (the class that is being inherited) and Bar the subclass (the class that does the inheriting). Notice that Foo also inherits from class, the object class. In Python, object is the top-level superclass everything inherits from it, whether directly or through other superclasses. object provides basic functionality that is needed for other classes to work with Python. 1.1 When should we use inheritance? One common use of inheritance is to represent a hierachcal relationship between two or more classes one class is a more specific version of the other class. For example, dogs are a specific type of pet, and a pet is a specific type of animal. Using inheritance, here is a second attempt at representing Dogs. class Pet(object): def init (self, name, owner): self.is_alive = True # It s alive!!! self.name = name self.owner = owner def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") print(... ) class Dog(Pet): def init (self, name, owner, color): Pet. init (self, name, owner) self.color = color print( woof! )

DISCUSSION 7: INHERITANCE AND INTERFACES Page 3 Notice that, by using inheritance, we did not have to redefine self.name, self.owner, or the eat method. We did, however, redefine the talk method in the Dog class. In this case, we want Dogs to talk differently, so we override the method. The line Pet. init (self, name, owner) in the Dog class is necessary for inheriting the instance attributes self.is alive, self.name, and self.owner. Without this line, Dog will never inherit those instance attributes. Notice that when we call Pet. init, we need to pass in self, since Pet is a class, not an instance. 1.2 Questions 1. Implement the Cat class by inheriting from the Pet class. Make sure to use superclass methods wherever possible. In addition, add a lose life method to the Cat class. class Cat(Pet): def init (self, name, owner, lives=9): """A cat says meow! when asked to talk.""" def lose_life(self): """A cat can only lose a life if they have at least one life. When lives reach zero, the is_alive variable becomes False. """

DISCUSSION 7: INHERITANCE AND INTERFACES Page 4 2. Assume these commands are entered in order. What would Python output? >>> class Foo(object):... def init (self, a):... self.a = a... def garply(self):... return self.baz(self.a) >>> class Bar(Foo):... a = 1... def baz(self, val):... return val >>> f = Foo(4) >>> b = Bar(3) >>> f.a >>> b.a >>> f.garply() >>> b.garply() >>> b.a = 9 >>> b.garply() >>> f.baz = lambda val: val * val >>> f.garply()

DISCUSSION 7: INHERITANCE AND INTERFACES Page 5 1.3 Extra Questions 1. More Cats! class NoisyCat(Cat): """A class that behaves just like a Cat, but always repeats things twice. """ def init (self, name, owner, lives=9): # Is this method necessary? Why or why not? """A NoisyCat will always repeat what he/she said twice. """ 2 Interfaces In computer science, an interface is a shared set of attributes, along with a specification of the attributes behavior. For example, an interface for vehicles might consist of the following methods: drive(self): Drives the vehicle if it has stopped. stop(self): Stops the vehicle if it is driving. Data types can implement the same interface in different ways. For example, a Car class and a Train can both implement the interface described above, but the Car probably has a different mechanism for drive than the Train. The power of interfaces is that other programs don t have to know how each data type implements the interface only that they have implemented the interface. The following travel function can work with both Cars and Trains: def travel(vehicle): while not at_destination(): vehicle.drive() vehicle.stop()

DISCUSSION 7: INHERITANCE AND INTERFACES Page 6 2.1 Interfaces in Python Python defines many interfaces that can be implemented by user-defined classes. For example, the interface for arithmetic consists of the following methods: add (self, other): Allows objects to do self + other. sub (self, other): Allows objects to do self - other. mul (self, other): Allows objects to do self * other. In addiiton, there is also an interface for sequences: len (self): Allows objects to do len(self). getitem (self, index): Allows objects to do self[i]. 2.2 Questions Let s implement a Vector class that support basic operations on vectors. These include adding and subtracting vectors of the same length, multiplying a vector with a scalar, and taking the dot product of two vectors. The results of these operations are shown in the table below: Operation Result -Vector([1, 2, 3]) Vector([-1, -2, -3]) Vector([1, 2, 3]) + Vector([4, 5, 6]) Vector([5, 7, 9]) Vector([4, 5, 6]) - Vector([1, 2, 3]) Vector([3, 3, 3]) Vector([1, 2, 3]) * Vector([1, 2, 3]) 14 Vector([1, 2, 3]) * 4 Vector([4, 8, 12]) 10 * Vector([1, 2, 3]) Vector([10, 20, 30]) len(vector([1, 2, 3])) 3 Vector([1, 2, 3])[1] 2 We begin with an implmentation of the Vector class: class Vector: def init (self, vector): self.vector = vector def neg (self) : "*** YOUR CODE HERE ***" def add (self, other): "*** YOUR CODE HERE ***" def sub (self, other): return self. add (-other) def mul (self, other): "*** YOUR CODE HERE ***" def rmul (self, other): return self. mul (other) def len (self) : return len(self.vector) def getitem (self, n) : return self.vector[n]

DISCUSSION 7: INHERITANCE AND INTERFACES Page 7 1. Implement neg, which returns a new Vector that is the negation of the current vector, self. Try using list comprehensions. def neg (self): return Vector( ) 2. Implement add, which takes in two vectors of the same length and returns a new vector which is their sum. Try using list comprehensions. def add (self, other): assert type(other) == Vector, "Invalid operation!" assert len(self) == len(other), "Invalid dimensions!" 3. Implement mul, which takes in a value, and performs a scalar product if the value is a number, or a vector product if the value is another vector. def mul (self, other): if type(other) == int or type(other) == float: "*** YOUR CODE HERE ***" elif type(other) == Vector: "*** YOUR CODE HERE ***"

DISCUSSION 7: INHERITANCE AND INTERFACES Page 8 2.3 Extra Questions 1. Now that we have a definition of a vector and its basic operations using type dispatching, we can write more complex expressions using Python s operator syntax. Length(v) = v = v v Norm(v) = v v Proj(u, v) = v u v v v Now write these vector functions using the Python operators we ve just defined. Notice how much cleaner this is compared to using function calls. from math import sqrt def vector_length(v): return def normalize(v): return def proj(u, v): return