This question is about the relationship between subtyping


Subtyping and Binary Methods

This question is about the relationship between subtyping and inheritance. Recall that the main principle associated with subtyping is substitutivity: If A is asubtype of B, then wherever a B object is required in a program, an A object may be used instead without producing a type error. For the purpose of this question, we will use Message not understood as our Smalltalk type error. This is the most common error message resulting from a dynamic type failure in Smalltalk; it is the object-oriented analog of the error Cannot take car of an atom that every Lisp programmer has generated at some time. Remember that Smalltalk is a dynamically typed language. This question asks you to show how substitutivity can fail, using the fact that a method given in a superclass can be rede?ned in a subclass.

If there are no restrictions on how a method (member function) may be rede?ned in a subclass, then it is easy to rede?ne a method so that it requires a different number of arguments. This will make it impossible to meaningfully substitute a subclass object for a superclass object. A more subtle fact is that subtyping may fail when a method is rede?ned in a way that appears natural and (unless you've seen this before) unproblematic. This is illustrated by the following Point class and ColoredPoint subclass:

Class name

Point

Class variables

 

Instance variables

xval yval

Instance messages and methods

xcoord ↑ xval

ycoord ↑ yval origin

xval ← 0

yval ← 0

movex: dx movey: dy xval ← xval + dx.   yval ← yval + dy equal: pt

↑ (xval = pt xcoord & yval = pt ycoord)

 

 

Class name

ColoredPoint

Class variables

 

Instance variables

color

Instance messages and methods

color ↑ color

changecolor: newc color ← newc equal: cpt

↑ (xval = cpt xcoord & yval = cpt ycoord &

color = cpt  color)

The important part here is the way that equal is rede?ned in the colored point class. This change would not be allowed in Simula or C++, but is allowed in Smalltalk. (The C++ compiler does not consider this an error, but it would not treat it as rede?nition of amember function either.) The intuitive reason for rede?ning equal is that two colored points are equal only if they have the same coordinates and are the same color.

Problem: Consider the expression p1 equal:p2, where p1 and p2 are either Point objects or ColoredPoint objects. It is guaranteed not to produce Message not under- stood if both p1 and p2 are either Points or ColoredPoints, but may produce an error if one is a Point and the other a ColoredPoint. Consider all four combinations of p1 and p2 as Points and ColoredPoints, and explain brie?y how each message is interpreted.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: This question is about the relationship between subtyping
Reference No:- TGS01269963

Expected delivery within 24 Hours