0
votes

I landed in a situation where I need to derive from an existing class that represents a tcp packet, lets call it PacketA. It contains length of the packet name and name itself in it's header so there are methods in PacketA class that helps to retrieve name and name length. Now I need to create another class let's call it PacketB which has identical properties as PacketA except it has no name and name length field. So I am thinking to derive PacketB from PacketA and override methods that have different behavior. Also plan is to throw an exception from methods that PacketB don't support, mainly name and name length.

This clearly don't follow Liskov substitution principle. Is there any way to implement this in cleaner way?

changed tag to c++ - Ashwini Taradale
Extract stuff common to both PacketA and PacketB to a common base class? Or perhaps derive PacketA from PacketB? - n. 1.8e9-where's-my-share m.