Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M91631525

Have any Question?


Related Questions in Java

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As