10.2 Inheritance
Objects are particularly well-suited to programs that involve modeling real or imaginary
worlds such as graphical user interfaces (modeling windows, files, and folders on
a desktop), simulations (modeling physical objects in the real world and their
interactions), and games (modeling creatures and things in an imagined world).
Objects in the real world (or most simulated worlds) are complex. Suppose we
are implementing a game that simulates a typical university.
It might include many different kinds of objects including places (which are stationary
and may contain other objects), things, and people.
There are many different kinds of people, such as students and professors. All objects in
our game have a name and a location; some objects also have methods for talking and
moving.
We could define classes independently for all of the object types, but this would involve
a lot of duplicate effort.
It would also make it hard to add a new behavior to all of the objects in the
game without modifying many different procedures.
The solution is to define more specialized kinds of objects using the definitions of other
objects. For example, a student is a kind of person.
A student has all the behaviors of a normal person, as well as some behaviors particular
to a studentsuch as choosing a major and graduating.
To implement a student class, we want to reuse methods from the person class without
needing to duplicate them in thestudent implementation.
We call the more specialized class (in this case thestudent class) the subclass and
say student is a subclass of person.
The reused class is known as the superclass, so person is the superclass of student.
A class can have many subclasses but only one superclass.1
Figure 10.2 illustrates some inheritance relationships for a university simulator.
The arrows point from subclasses to their superclass.
A class may be both a subclass to another class, and a superclass to a different class. For
example,person is a subclass of movable-object, but a superclass of student andprofessor.
Figure 10.2: Inheritance hierarchy.
Our goal is to be able to reuse superclass methods in subclasses.
When a method is invoked in a subclass, if the subclass does not provide a definition of
the method, then the definition of the method in the superclass is used.
This can continue up the superclass chain. For instance, student is a subclass of person,
which is a subclass of movable-object, which is a subclass of sim-object(simulation object),
which is the superclass of all classes in the simulator.
Hence, if the sim-object class defines a get-name method, when the get-namemethod
is invoked on a student object, the implementation of get-name in thesim-object class will
be used (as long as neither person nor movable-objectdefines its own get-name method).
When one class implementation uses the methods from another class we say the
subclass inherits from the superclass.
Inheritance is a powerful way to obtain many different objects with a small amount of
code.
10.2.1 Implementing Subclasses
To implement inheritance we change class definitions so that if a requested method is
not defined by the subclass, the method defined by its superclass will be used.
The make-sub-object procedure does this. It takes two inputs, a superclass object and the
object dispatching procedure of the subclass, and produces an instance of the subclass
which is a procedure that takes a message as input and outputs the
method corresponding to that message.
If the method is defined by the subclass, the result will be the subclass method. If the
method is not defined by the subclass, it will be the superclass method.
(define (make-sub-object
(lambda (message)
super subproc)
(let ((method (subproc
(if method method (super message)))))
message)))
When an object produced by (make-sub-object obj proc) is applied to a message, it
first applies the subclass dispatch procedure to the message to find an appropriate
method if one is defined.
If no method is defined by the subclass implementation, it evaluates to (super message),
the method associated with themessage in the superclass.
References to self. It is useful to add an extra parameter to all methods so the object
on which the method was invoked is visible.
Otherwise, the object will lose its special behaviors as it is moves up the superclasses.
We call this the selfobject (in some languages it is called the this object instead).
To support this, we modify the ask procedure to pass in the object parameter to
the method:
(define (ask object message . args) (apply (object message) object
args))
All methods now take the self object as their first parameter, and may take
additional parameters. So, the counter constructor is defined as:
(define (make-counter) (let ((count 0))
(lambda (message)
(cond
((eq? message 'get-count) (lambda (self) count))
((eq? message 'reset!) (lambda (self) (set! count 0)))
((eq? message 'next!) (lambda (self) (set! count (+ 1 count))))
(else (error "Unrecognized message"))))))
Subclassing counter. Since subclass objects cannot see the instance variables of their
superclass objects directly, if we want to provide a versatile counter class we need to also
provide a set-count! method for setting the value of the counter to an arbitrary value.
For reasons that will become clear later, we should useset-count! everywhere the value of
the count variable is changed instead of setting it directly:
(define (make-counter)
(let ((count 0))
(lambda (message)
(cond 'get-count)
(lambda (self)
count))
val) (set! count
val)))
((eq? message
'set-count!) (lambda (self((eq? message
(else (error "Unrecognized message"))))))
Previously, we defined make-adjustable-counter by repeating all the code
frommake- counter and adding an adjust! method.
With inheritance, we can definemake-adjustable-counter as a
counter without repeating any code:
subclass of make-
(define (make-adjustable-counter)
(make-sub-object
(make-counter)
(lambda (message)
(cond 'adjust!)
val) 'get-count)
val))))
((eq? message
(lambda (self
(ask self 'set-count! (+ (ask self
(else false)))))
We use make-sub-object to create an object that inherits the behaviors from one
class, and extends those behaviors by defining new methods in the subclass
implementation.
((eq? message 'reset!) (lambda (self) (ask self 'set-count! 0)))
((eq? message 'next!)
(lambda (self) (ask self 'set-count! (+ 1 (ask self 'current)))))
10.2.2 Overriding methods
In addition to adding new methods, subclasses can replace the definitions of methods
defined in the superclass.
When a subclass replaces a method defined by its superclass, then the subclass method
the superclass method. When the method is invoked on a subclass object, the
new method will be used.
For example, we can define a subclass of reversible-counter that is not allowed to
have negative counter values.
If the counter would reach a negative number, instead of setting the counter to the new
value, it produces an error message and maintains the counter at zero. We do this by
overriding the set-count! method, replacing the superclass implementation of the
method with a new implementation.
(define (make-positive-counter)
(make-subobject
(make-reversible-counter)
(lambda (message)
(cond 'set-count!)
((eq? message
(lambda (self val) (if (< val 0) (error "Negative count")
...)))
(else false)))))
What should go in place of the ……? When the value to set the count to is not negative,
what should happen is the count is set as it would be by the superclassset-count! method.
In the positive-counter code though, there is no way to access the count variable since it is
in the superclass procedure's application environment.
There is also no way to invoke the superclass' set-count! method since it has been
overridden by positive-counter.
The solution is to provide a way for the subclass object to obtain its superclass object.
We can do this by adding a get-super method to the object produced bymake-sub-object:
(define (make-sub-object
(lambda (message)
super subproc)
(if (eq? message 'get-super)
(lambda (self) super)
(let ((method (subproc
(if method method (super message))))))
message)))
Thus, when an object produced by make-sub-object is passed the get-supermessage it
returns a method that produces the super object.
The rest of the procedure is the same as before, so for every other message it behaves
like the earlier make-sub-object procedure.

More Related Content

PPT
JVM and OOPS Introduction
PPT
Chapter 1 Presentation
PPTX
Java String
PPT
packaging procedures_and_state
PPTX
Dynamic databinding
DOCX
java classes
PPTX
Chain of responsibility pattern
PPS
String and string buffer
JVM and OOPS Introduction
Chapter 1 Presentation
Java String
packaging procedures_and_state
Dynamic databinding
java classes
Chain of responsibility pattern
String and string buffer

What's hot (20)

PPT
Unit4 (2)
PPTX
PDF
Arrays string handling java packages
PDF
The Ruby Object Model by Rafael Magana
PPT
Objects by Sufian Idris
PPTX
Vectors in Java
PPT
String handling session 5
PDF
Action Script
PPTX
Only oop
PPT
Chain of responsibility
PPTX
Lightning talk
PPTX
Java Beans Unit 4(Part 1)
PDF
Anton Kasyanov, Introduction to Python, Lecture3
PDF
Java String
PPTX
Interesting Facts About Javascript
PDF
PPTX
Java Unit 2 (Part 2)
PPTX
String, string builder, string buffer
PPTX
웹기반 Ajax개발을 위한 프레임워크 - metaworks3 (메타웍스3)
PPTX
Javascript Objects Deep Dive
Unit4 (2)
Arrays string handling java packages
The Ruby Object Model by Rafael Magana
Objects by Sufian Idris
Vectors in Java
String handling session 5
Action Script
Only oop
Chain of responsibility
Lightning talk
Java Beans Unit 4(Part 1)
Anton Kasyanov, Introduction to Python, Lecture3
Java String
Interesting Facts About Javascript
Java Unit 2 (Part 2)
String, string builder, string buffer
웹기반 Ajax개발을 위한 프레임워크 - metaworks3 (메타웍스3)
Javascript Objects Deep Dive
Ad

Viewers also liked (19)

PPT
mutable pairs_and_lists
PPTX
processes
PPT
histry of_computing_machines
PPT
expressions
PPT
developing complex_programs
PPT
languages
PPT
parser
PPT
decisions
PPT
modeling computing
PPT
1.3 applications, issues
PPT
recursive transition_networks
PPTX
measuring computing_power
PPTX
Regular expressions h1
PPT
Binary trees
PPT
Quicksort
PPTX
Normal forms cfg
PPT
TM - Techniques
PPTX
Pumping lemma for regular set h1
PPT
Basics of data structure
mutable pairs_and_lists
processes
histry of_computing_machines
expressions
developing complex_programs
languages
parser
decisions
modeling computing
1.3 applications, issues
recursive transition_networks
measuring computing_power
Regular expressions h1
Binary trees
Quicksort
Normal forms cfg
TM - Techniques
Pumping lemma for regular set h1
Basics of data structure
Ad

Similar to inheritance (20)

PDF
Preexisting code, please useMain.javapublic class Main { p.pdf
PPTX
Module-5-Classes and Objects for Python Programming.pptx
PPTX
Inheritance and Polymorphism in java simple and clear
PDF
java_inheritance.pdf
PDF
Class notes(week 6) on inheritance and multiple inheritance
PPTX
Inheritance
PPT
JAVA Polymorphism
DOC
Delphi qa
PPTX
Presentation 3rd
PPT
Classes2
PPT
CHAPTER 1 - OVERVIEW OOP.ppt
PDF
Utility Classes
PPT
6. Compile And Run
PDF
Object Oriented PHP - PART-1
PPTX
Java basics
PPTX
MODULE_3_Methods and Classes Overloading.pptx
PDF
Copy on write
PPT
Classes1
PPSX
Oop features java presentationshow
DOC
Abstract
Preexisting code, please useMain.javapublic class Main { p.pdf
Module-5-Classes and Objects for Python Programming.pptx
Inheritance and Polymorphism in java simple and clear
java_inheritance.pdf
Class notes(week 6) on inheritance and multiple inheritance
Inheritance
JAVA Polymorphism
Delphi qa
Presentation 3rd
Classes2
CHAPTER 1 - OVERVIEW OOP.ppt
Utility Classes
6. Compile And Run
Object Oriented PHP - PART-1
Java basics
MODULE_3_Methods and Classes Overloading.pptx
Copy on write
Classes1
Oop features java presentationshow
Abstract

More from Rajendran (20)

PPT
Element distinctness lower bounds
PPT
Scheduling with Startup and Holding Costs
PPT
Divide and conquer surfing lower bounds
PPT
Red black tree
PPT
Hash table
PPT
Medians and order statistics
PPT
Proof master theorem
PPT
Recursion tree method
PPT
Recurrence theorem
PPT
Master method
PPT
Master method theorem
PPT
Hash tables
PPT
Lower bound
PPT
Master method theorem
PPT
Greedy algorithms
PPT
Longest common subsequences in Algorithm Analysis
PPT
Dynamic programming in Algorithm Analysis
PPT
Average case Analysis of Quicksort
PPT
Np completeness
PPT
computer languages
Element distinctness lower bounds
Scheduling with Startup and Holding Costs
Divide and conquer surfing lower bounds
Red black tree
Hash table
Medians and order statistics
Proof master theorem
Recursion tree method
Recurrence theorem
Master method
Master method theorem
Hash tables
Lower bound
Master method theorem
Greedy algorithms
Longest common subsequences in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Average case Analysis of Quicksort
Np completeness
computer languages

Recently uploaded (20)

PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
International_Financial_Reporting_Standa.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Trump Administration's workforce development strategy
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Environmental Education MCQ BD2EE - Share Source.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
FORM 1 BIOLOGY MIND MAPS and their schemes
International_Financial_Reporting_Standa.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Uderstanding digital marketing and marketing stratergie for engaging the digi...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Hazard Identification & Risk Assessment .pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Introduction to pro and eukaryotes and differences.pptx
Computer Architecture Input Output Memory.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
Trump Administration's workforce development strategy
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
AI-driven educational solutions for real-life interventions in the Philippine...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...

inheritance

  • 1. 10.2 Inheritance Objects are particularly well-suited to programs that involve modeling real or imaginary worlds such as graphical user interfaces (modeling windows, files, and folders on a desktop), simulations (modeling physical objects in the real world and their interactions), and games (modeling creatures and things in an imagined world). Objects in the real world (or most simulated worlds) are complex. Suppose we are implementing a game that simulates a typical university. It might include many different kinds of objects including places (which are stationary and may contain other objects), things, and people. There are many different kinds of people, such as students and professors. All objects in our game have a name and a location; some objects also have methods for talking and moving. We could define classes independently for all of the object types, but this would involve a lot of duplicate effort. It would also make it hard to add a new behavior to all of the objects in the game without modifying many different procedures. The solution is to define more specialized kinds of objects using the definitions of other objects. For example, a student is a kind of person.
  • 2. A student has all the behaviors of a normal person, as well as some behaviors particular to a studentsuch as choosing a major and graduating. To implement a student class, we want to reuse methods from the person class without needing to duplicate them in thestudent implementation. We call the more specialized class (in this case thestudent class) the subclass and say student is a subclass of person. The reused class is known as the superclass, so person is the superclass of student. A class can have many subclasses but only one superclass.1 Figure 10.2 illustrates some inheritance relationships for a university simulator. The arrows point from subclasses to their superclass. A class may be both a subclass to another class, and a superclass to a different class. For example,person is a subclass of movable-object, but a superclass of student andprofessor. Figure 10.2: Inheritance hierarchy.
  • 3. Our goal is to be able to reuse superclass methods in subclasses. When a method is invoked in a subclass, if the subclass does not provide a definition of the method, then the definition of the method in the superclass is used. This can continue up the superclass chain. For instance, student is a subclass of person, which is a subclass of movable-object, which is a subclass of sim-object(simulation object), which is the superclass of all classes in the simulator. Hence, if the sim-object class defines a get-name method, when the get-namemethod is invoked on a student object, the implementation of get-name in thesim-object class will be used (as long as neither person nor movable-objectdefines its own get-name method). When one class implementation uses the methods from another class we say the subclass inherits from the superclass. Inheritance is a powerful way to obtain many different objects with a small amount of code.
  • 4. 10.2.1 Implementing Subclasses To implement inheritance we change class definitions so that if a requested method is not defined by the subclass, the method defined by its superclass will be used. The make-sub-object procedure does this. It takes two inputs, a superclass object and the object dispatching procedure of the subclass, and produces an instance of the subclass which is a procedure that takes a message as input and outputs the method corresponding to that message. If the method is defined by the subclass, the result will be the subclass method. If the method is not defined by the subclass, it will be the superclass method. (define (make-sub-object (lambda (message) super subproc) (let ((method (subproc (if method method (super message))))) message))) When an object produced by (make-sub-object obj proc) is applied to a message, it first applies the subclass dispatch procedure to the message to find an appropriate method if one is defined. If no method is defined by the subclass implementation, it evaluates to (super message), the method associated with themessage in the superclass.
  • 5. References to self. It is useful to add an extra parameter to all methods so the object on which the method was invoked is visible. Otherwise, the object will lose its special behaviors as it is moves up the superclasses. We call this the selfobject (in some languages it is called the this object instead). To support this, we modify the ask procedure to pass in the object parameter to the method: (define (ask object message . args) (apply (object message) object args)) All methods now take the self object as their first parameter, and may take additional parameters. So, the counter constructor is defined as: (define (make-counter) (let ((count 0)) (lambda (message) (cond ((eq? message 'get-count) (lambda (self) count)) ((eq? message 'reset!) (lambda (self) (set! count 0))) ((eq? message 'next!) (lambda (self) (set! count (+ 1 count)))) (else (error "Unrecognized message")))))) Subclassing counter. Since subclass objects cannot see the instance variables of their superclass objects directly, if we want to provide a versatile counter class we need to also provide a set-count! method for setting the value of the counter to an arbitrary value.
  • 6. For reasons that will become clear later, we should useset-count! everywhere the value of the count variable is changed instead of setting it directly: (define (make-counter) (let ((count 0)) (lambda (message) (cond 'get-count) (lambda (self) count)) val) (set! count val))) ((eq? message 'set-count!) (lambda (self((eq? message (else (error "Unrecognized message")))))) Previously, we defined make-adjustable-counter by repeating all the code frommake- counter and adding an adjust! method. With inheritance, we can definemake-adjustable-counter as a counter without repeating any code: subclass of make- (define (make-adjustable-counter) (make-sub-object (make-counter) (lambda (message) (cond 'adjust!) val) 'get-count) val)))) ((eq? message (lambda (self (ask self 'set-count! (+ (ask self (else false))))) We use make-sub-object to create an object that inherits the behaviors from one class, and extends those behaviors by defining new methods in the subclass implementation. ((eq? message 'reset!) (lambda (self) (ask self 'set-count! 0))) ((eq? message 'next!) (lambda (self) (ask self 'set-count! (+ 1 (ask self 'current)))))
  • 7. 10.2.2 Overriding methods In addition to adding new methods, subclasses can replace the definitions of methods defined in the superclass. When a subclass replaces a method defined by its superclass, then the subclass method the superclass method. When the method is invoked on a subclass object, the new method will be used. For example, we can define a subclass of reversible-counter that is not allowed to have negative counter values. If the counter would reach a negative number, instead of setting the counter to the new value, it produces an error message and maintains the counter at zero. We do this by overriding the set-count! method, replacing the superclass implementation of the method with a new implementation. (define (make-positive-counter) (make-subobject (make-reversible-counter) (lambda (message) (cond 'set-count!) ((eq? message (lambda (self val) (if (< val 0) (error "Negative count") ...))) (else false))))) What should go in place of the ……? When the value to set the count to is not negative, what should happen is the count is set as it would be by the superclassset-count! method. In the positive-counter code though, there is no way to access the count variable since it is in the superclass procedure's application environment.
  • 8. There is also no way to invoke the superclass' set-count! method since it has been overridden by positive-counter. The solution is to provide a way for the subclass object to obtain its superclass object. We can do this by adding a get-super method to the object produced bymake-sub-object: (define (make-sub-object (lambda (message) super subproc) (if (eq? message 'get-super) (lambda (self) super) (let ((method (subproc (if method method (super message)))))) message))) Thus, when an object produced by make-sub-object is passed the get-supermessage it returns a method that produces the super object. The rest of the procedure is the same as before, so for every other message it behaves like the earlier make-sub-object procedure.