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

  How should ijvm be modified to make best use of instructions

On the full JVM machine, there are special 1-byte opcodes for loading locals 0 through 3 onto the stack instead of using the general ILOAD instruction. How should IJVM be modified to make the best use of these instructions?

  Creates a list of shapes stored in an array

Identify and use the correct syntax of a common programming language (Java) - describe program functionality based on analysis of a given program;

  Create a java class called student

Create a Java class called student with the following instance variables: private String studentName

  What is the relationship between an exception and signaling

What is the relationship between an exception, signaling and an exception handler

  Create a method to calculate the value of the inventory

Create another method to sort the array items by the name of the product.

  Develop and test stand alone java applications

Demonstrate an understanding of Object-Oriented Programming concepts in Java.

  Convert the following expression to postfix

Convert the following expression to postfix. ( 5 * ( ( 9 * 8 ) + ( 7 * ( 4 + 6 ) ) ) )

  When the user presses a button

When the user presses a button, get the text from text field. Now extract the words from the text one at a time and find the largest word of length 4 or more. Let's call this largest word X for now. In response, create a question based on the length ..

  Calculate the cross product of two numeric vectors function

Create a function that calculate the cross product of two numeric vectors Function Name: calcCrossProduct

  Create the class airplane

Your missions is to create the class Airplane. Each Airplane object should store the following information (fields):

  Disadvantages to using data structures

How are data structures managed and organized in this language. How does the management or syntax of data structures in this language differ from how data structures are handled in other language.

  Elements in a two-dimensional array

Write a value-returning method that returns the number of columns with n elements in a two-dimensional array of chars, where n is a parameter of the method. Include code to test your method.

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