Subtyping and binary methods

Assignment Help JAVA Programming
Reference no: EM13963505

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.

Reference no: EM13963505

Questions Cloud

Multiple inheritance and casts : An important aspect of C++ object and virtual function table (vtbl) layout is that if class D has class B as a public base class, then the initial segment of every D object must look like a B object, and similarly for the D and B virtual function ..
Consider two spin systems a and a'' placed in external field : In the most probable situation corresponding to the final thermal equilibrium, how is the energy Et of systemA related to the energy Et' of system A' ?
How elaborating evolution helps explain social change : Post an explanation of how elaborating evolution helps explain social change. Then, explain how elaborating evolutionary systems might inform how you, as a leader or manager, can enact positive social change
Find recent news that involves information security : For this assignment, you need to find recent news that involves information security breaches. This news must not be older than one year. Then you need to analyse the chosen news and write a report on it. The report should include but not limited to ..
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
What is the total solar power received by the box : Kyoto box is a simple solar oven used extensively in Africa. Assuming a square box with four reflectors of L = 75 cm, on a sunny day with direct sunlight from the zenith, what is the optimum angle ?? What is the total solar power received by the b..
Relationship between inheritance and subtyping : With this declaration of m in subclass B, any invocation of m on a B object will result in a special error indicating that the method should not be used. (a) What effect does this feature of Smalltalk have on the relationship between inherit..
What is the standard deviation of the annual salary : What is Bob's average annual earnings. In the previous question, what is the standard deviation of the annual salary? What is the probability that John will get 2 tickets during the week (Monday-Friday)?
Perceptions of acl rehabilitation : The purpose of this study is obtaining undiscovered perceptions of strength and conditioning coaches (S&C) and physiotherapists', on what defines a successful return to sport following an anterior cruciate ligament (ACL) injury.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a java applet that views staff information

Write a Java applet that views, inserts, and updates staff information stored in a table Staff in an Oracle Database. The View button displays a record with a specified ID

  Describe the steps to program development

What is the function of parseFloat and parseInt and what will be result if we send "abcd" through a prompt() input and pass it through parseInt()?

  Write a mathematical recursive definition

Write a mathematical, recursive definition of p(n) - Prove that your answer to Part a is correct by using mathematical induction.

  Complier error and run time error

Q1. What do you understand by complier error and run time error? Explain by example.

  Update your website to include a simple web form

Update your website to include a simple web form as discussed in your design document. Use JavaScript to validate the form contents and display the results of that validation to the user in a popup window

  Write a version of sumpairs

Write a version of sumPairs  that sums each component of the pairs separately, returning a pair consisting of the sum of the first components and the sum of the second components. So basically [(3,1)(10,3)] would return (13,4).

  Implement a modified version of the card game war

Implement a modified version of the card game WAR

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Write a java program to read sequences of integers from a

write a java program to read sequences of integers from a text file build a binary search tree for each sequence by

  Design implement test and debug a program with a jframe

design implement test and debug a program with a jframe that allows the user to enter a series of contacts names ages

  Write a method insert at that takes an array of integers

Write a method, insertAt, that takes four parameters: an array of integers; the length of the array; an integer, say, insertItem; and an integer, say, index

  Write java method which fills the array with random numbers

Write a Java method which takes an integer array parameter and fills the array with random numbers between 1 and 1000, including 1 and 1000.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd