You are on page 1of 2

Khanh Pham

CS 132
HW #5

1.

Sandwich x = new Sandwich();
Sub y = new Sub();

Since the class Sub extends the class Sandwich, the class Sandwich will be the
Superclass and the class Sub will be the subclass.

a.
x = y;
This is legal because you the super class Sandwich has every fields and methods that
the Sub class has.
d.
x = new Sub();
This is legal the Sandwich class has all methods and fields that the Sub class has =>
we can put the Sub class into the Sandwich class.

2.













3.
a. pm.getMessageType(): Text Message

The snippets generate this output because the class PaymentMessage doesnt have
the getMessageType() method. Therefore, it has to call the method from the super
class TextMessage. Since the type of object determines the code that gets executed,
the snippets will generate the output TextMessage.

b. The code in the class PaymentMessage:

public String getMessageType() {
Person
Student
Instructor
Employee
return "Payment Message";
}

c.

public PaymentMessage(long fromNumber, long toNumber, String msg)
{
super(fromNumber, toNumber, msg + " ");
paymentAmount = 0;
}


public PaymentMessage(long fromNumber, long toNumber, String msg,
double amt) {
super(fromNumber, toNumber, msg + " ");
paymentAmount = amt;
}

You might also like