Follow a call inside an inherited method

A device simulator models packet sizes with the following Java classes. A frame contains two copies of its packet's payload plus three bytes of framing information. Every size returned by a method is measured in bytes.


Java
class Packet {
    int payloadBytes() {
        return 11;
    }

    int frameBytes() {
        return 2 * payloadBytes() + 3;
    }
}

class PriorityPacket extends Packet {
    @Override
    int payloadBytes() {
        return 18;
    }
}

class CompactPacket extends Packet {
    @Override
    int payloadBytes() {
        return 7;
    }
}

Packet standard = new Packet();
Packet priority = new PriorityPacket();
Packet compact = new CompactPacket();


Complete the table with the exact integer returned by each call. All three variables are declared as Packet, and neither subclass declares its own frameBytes method.

CallReturned size (bytes)
standard.frameBytes()
priority.frameBytes()
compact.frameBytes()

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Polymorphism Questions: