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

  Calculates and displays the mortgage payment amount

Write a Java program without a graphical user interface that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. In this program, hard code the amou..

  Java problem - g queue

A queue is an ordered collection of items in which the removal of items is restricted to the FIFO ( rst in rst out) principle.

  Implement client-server communication

Write a java program implementing Client-Server communication using Remote Method Invocation.

  Your task in this assignment is to develop a graphical user

your task in this assignment is to develop a graphical user interface based java program that can communicate with a

  Implement a set as a new class that is derived

A set is like a bag, except that a set does not allow multiple copies of any element. If you try to insert a new copy of an item that is already present in a set, then the set simply remains unchanged.

  Describe the project requirements and other assumptions

Summarize the project requirements and other assumptions (i.e., budget, human resources, workload, environmental dependencies, etc.). Define a work breakdown structure and describe the methodology behind constructing one

  Modify the java application using java netbeans and ide

Modify the Java application using Java NetBeans IDE to meet these additional and changed business requirements: The company has recently changed its total annual compensation policy to improve sales.

  Threads and synchronization

Threads and Synchronization- Whenever we have multiple entities working together to get something done, we have the problem of synchronization between the entities

  Write a test program that obtains from the user

Write a test program that obtains from the user the items to store in two sets A and B, and displays the union A u B, the inter- section A n B, and the dierence A \ B. The data type of the items is your choice. ( you can only store objects.)

  Write a java class that creates an array of integers

Write a Java class that creates an array of integers, fills it with values, prints the unsorted values, sorts the values into ascending order, and finally prints the sorted values.

  Write the java code for an abstract class named account

Write the Java code for an abstract class named Account which has two data members; one for the account number and the other for the account balance (use information hiding). The Account class should include get and set methods for both data membe..

  Calculate payback period and net present value

Calculate payback period (PBP), net present value (NPV), and internal rate of return (IRR). Which project do you recommend for development? Support your recommendation.

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