Understanding Inheritance and Subclasses in Object-Oriented Programming, Slides of Introduction to Computing

The concept of inheritance and subclasses in object-oriented programming using the example of a drawing program. It covers defining classes for different types of slide content, creating subclasses, and the importance of names in subclasses and superclasses. The document also discusses customizing classes and the process of initialization.

Typology: Slides

2012/2013

Uploaded on 08/17/2013

bakul
bakul 🇮🇳

4.6

(16)

69 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Program design!
Example: drawing program (e.g. PowerPoint)!
Many types of content can appear on slides!
Want do do things like"
for x in slide[i].contents:!
x.draw(window)"
No problem: define class for every type of
content (text box, rectangle, image, …), make
sure each has a draw method!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Understanding Inheritance and Subclasses in Object-Oriented Programming and more Slides Introduction to Computing in PDF only on Docsity!

Program design

• Example: drawing program (e.g. PowerPoint)

• Many types of content can appear on slides

• Want do do things like

for x in slide[i].contents:!

x.draw(window)

• No problem: define class for every type of

content (text box, rectangle, image, …), make

sure each has a draw method

Sharing work

• Defining separate classes for text box, image,

etc. is fine, but could get repetitive

§ all have code for drawing selection handles, frames,

backgrounds, …

• Solution: make these shapes subclasses of a

single class, where the shared code lives

Names in subclasses and superclasses

• Recall rule for looking up attribute names in classes:

look first in the instance, then in the class.

• With inheritance, there’s one simple addition: look in

the instance, then in the class, then in the superclass.

init(x,y,w,h) draw_frame() select()

SC

init(x,y,text) draw() TextBox(SC) TextBox text ‘Hi!’ id p (^) id p.text (^) p.draw() p.select()

Customizing a class

• Example: telephony program (e.g. Skype)

• Call and Hang Up buttons should be green and

red (to follow convention from cell phones)

• Already have a class for normal buttons

• Implement from scratch? No, what a waste…

• Instead create a subclass of the button class that

is just like a normal button, except it draws itself

with a different color.

Inheritance

• Superclass also called “parent”

• If subclass does nothing special, it has all the

same attributes as the parent class—it inherits all

the methods and variables

• Subclass can add new methods and variables

(with different names)

• Subclass can override methods and class

variables (by giving them the same names)

Review: names and instances

class A(object): x = 29 y = 42 def init(self): self.y = 2 self.z = 3 def f(self): print 'this is A.f' print 'self.x:', self.x print 'self.y', self.y print 'self.z', self.z print 'A.y', A.y a = A() print 'a.y:', a.y print 'A.y:', A.y a.f() A.f(a) which appears? (A) a.y: 42 (B) a.y: 29 (C) a.y: 2 (D) an error which appears? (A) A.y: 42 (B) A.y: 29 (C) A.y: 2 (D) an error which appears? (A) self.y: 42 (B) self.y: 29 (C) self.y: 2 (D) an error The two calls (A) do the same thing to A.f: (B) first is an error (C) second is an error (D) there are not two calls

Initialization

• We haven’t said anything about instance

variables—are they inherited too?

• Remember instance variables are created during

initialization (or at other times but that is not a good idea)

• To create new instance variables in the subclass

we need a subclass initializer

• For the superclass to work correctly we still need

the superclass initializer

• How is this going to work?

Subclass initialization example

class SlideContent(object): """Any object on a slide.""" def init(self, x, y, w, h): """Obj. with given pos'n and size""" self.x = x; self.y = y self.w = w; self.h = h … class TextBox(SlideContent): """An object containing text.""" def init(self, x, y, text): w = width(text) h = height(text) SlideContent.init(self, x, y, w, h) self.text = text … class Image(SlideContent): """An image.""" def init(self, x, y, image_file): … … SlideContent initializer sets up instance variables for the position and size of the object on the slide TextBox initializer overrides the superclass initializer. Only the TextBox initializer is called to initialize a fresh TextBox instance. To ensure that the superclass still gets initialized, the subclass initializer must call the superclass initializer explicitly. In this example the size is computed in the initializer.

Summary: defining a subclass

• Methods and class variables in the superclass can be

overridden by definitions in the subclass

§ you can still get at them by accessing them explicitly through

the superclass

• Instance variables set by the superclass initializer can

be overwritten by initializations in the subclass

• Always call the superclass initializer from the subclass

initializer, before initializing the subclass. Then these

two not only sound similar but also act similarly!