SDSU CS 535 Object-Oriented Programming
Fall Semester, 2003
Classes 2 Appendix
Previous    Lecture Notes Index    Next    
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 18-Sep-03


Initialization and Inheritance

Smalltalk.Core defineClass: #Parent
   superclass: #{Core.Object}
   indexedType: #none
   private: false
   instanceVariableNames: 'foo '
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'

Class Method

new
   ^super new initialize

Instance Methods

initialize
   foo :=6.

foo
   ^foo



Doc 6, Classes 2 Appendix Slide # 2
Initialization of Subclass

How to initialize bar?

Smalltalk.Core defineClass: #Child
   superclass: #{Core.Parent}
   indexedType: #none
   private: false
   instanceVariableNames: 'bar '
   classInstanceVariableNames: ''
   imports: ''
   category: 'CS535'

Bad Idea 1 – Use Same pattern

Child class>>new
   ^super new initialize

Child>>initialize
   bar := 2.

Child>>bar
   ^bar


Doc 6, Classes 2 Appendix Slide # 3
Why bad?


| test |
test := Child new.
test foo “returns nil”


Child class>>new is not needed
Child class inherits an identical method


Doc 6, Classes 2 Appendix Slide # 4
Bad Idea 2 – Subclass initializes Parent Variable

Child>>initialize
   bar := 2.
   foo := 6.

Child>>bar
   ^bar

Why Bad?

Child class now involved in private affairs of the Parent

Changes to the Parent instance variables require changing Child


Doc 6, Classes 2 Appendix Slide # 5
Solution

Child>>initialize
   super initialize
   bar := 2.

Child>>bar
   ^bar


Copyright ©, All rights reserved.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 18-Sep-03    Next