Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started