Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Superclass and subclass: I executed the code bellow, which is the same as the one shown in the Java How to Program 8th book, as

Superclass and subclass:

I executed the code bellow, which is the same as the one shown in the Java How to Program 8th book, as an example of polymorphism. The error occurs on the line where I marked a comment of 'ERROR !!' in a row that assigns the reference of the basePlusCommission subclass object to a superclass CommissionEmployee variable. Please help. Thanks in advance.

//Fig10.1: PolymorphismTest.java

package com.deitel.jhtp.cap10;

import com.deitel.jhtp.cap09.ComissionEmployee;

import com.deitel.jhtp.cap09.BasePlusComissionEmployee;

public class PolymorphismTest {

public static void main(String[] args) {

ComissionEmployee comissionEmployee = new ComissionEmployee("Sue", "Jones", "222-22-2222", 10000, .06);

BasePlusComissionEmployee basePlusCommissionEmployee = new BasePlusComissionEmployee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);

System.out.printf("%s %s: %s ",

"Call CommissionEmployee's toString with superclass reference ",

"to superclass object", comissionEmployee.toString());

System.out.printf("%s %s: %s ",

"Call BasePlusComissionEmployee's toString with subclass ",

"reference to subclass object", basePlusCommissionEmployee.toString());

ComissionEmployee commissionEmployee2 = basePlusCommissionEmployee; // ERROR !!

System.out.printf("%s %s: %s ",

"Call BasePlusCommissionEmployee's toString with superclass",

"reference to subclass object", commissionEmployee2.toString());

}//main(-)

}//class(-)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions

Question

6. Be able to choose and prepare a training site.

Answered: 1 week ago