I have now put another rephrased version of original question as requested by the user in the comments with class names mimicking real world scenario of postal office (though I dont know how real world the rephrased example is). Original question had abstract/generalized class names. Also uploaded the correct code for both.Rephrased QuestionDesign RequirementsPostage and PostageLot: We run a postal office. So we have class Postage and interface PostageLot.Classifying postage lot based on required speed of delivery: There is class SpeedPostageLot and class NormalPostageLot that implement interface PostageLot.Classifying postage lot based on care to be taken:There are interface ExecutivePostageLot and interface PublicPostageLot. ExecutivePostageLot is given extra care than PublicPostageLot. For illustration in code, I considered only ExecutivePostageLot and did not wrote PublicPostageLot, so we will not write corresponding sub-classes for PublicPostageLotBoth executive and public postages can be speed or normal postage. Thus I created subclasses of SpeedPostageLot and NormalPostageLot that implement ExecutivePostageLot: ExecutiveSpeedPostageLot and ExecutiveNotmalPostageLot. (Same in case for public postages: PublicSpeedPostageLot and PublicNormalPostageLot)There is also ExecutivePostage classThere is also CommonSpeedPostageProcessor which contains common logic to process SpeedPostageLot which can be used by both ExecutivePostageProcessor and PublicPostageProcessor when they deal with SpeedPostageLot. The methods of CommonSpeedPostageLotProcessor should return values of type which can be cast to ExecutiveSpeedPostageLot and PublicSpeedPostageLot. I felt this should be PostageLot or SpeedPostageLot since it does not have any connotation of care-to-be taken (executive or public) attached to it. But this is not working as can be seen in below code.Codepublic class Postage {}public interface PostageLot<T extends Postage> {}public abstract class NormalPostageLot<T extends Postage> implements PostageLot<T>{}public abstract class SpeedPostageLot<T extends Postage> implements PostageLot<T>{}public interface ExecutivePostageLot extends PostageLot<ExecutivePostage> {}public class ExecutiveSpeedPostageLot extends SpeedPostageLot<ExecutivePostage> implements ExecutivePostageLot {}public class ExecutiveNormalPostageLot extends NormalPostageLot<ExecutivePostage> implements ExecutivePostageLot {}public class ExecutivePostage extends Postage { }public class CommonSpeedPostageProcessor {    PostageLot<Postage> speedPostageProcessor1()    {        SpeedPostageLot<Postage> obj = null;        return obj;    }    SpeedPostageLot<Postage> speedPostageProcessor2()    {        SpeedPostageLot<Postage> obj = null;        return obj;    }}public class ExecutivePostageLotHandler {    public static void main(String[] args) {        CommonSpeedPostageProcessor logic = new CommonSpeedPostageProcessor();        ExecutivePostageLot obj = logic.method1EntitySetProcessor1(); //Type mismatch: cannot convert from PostageLot<Postage> to ExecutivePostageLot        ExecutivePostageLot obj2 = logic.method1EntitySetProcessor2(); //Type mismatch: cannot convert from SpeedPostage<Postage> to ExecutivePostageLot        ExecutiveSpeedPostageLot obj3 = logic.method1EntitySetProcessor1(); //Type mismatch: cannot convert from PostageLot<Postage> to ExecutiveSpeedPostageLot        ExecutiveSpeedPostageLot obj4 = logic.method1EntitySetProcessor2(); //Type mismatch: cannot convert from SpeedPostage<Postage> to ExecutiveSpeedPostageLot    }}(The zip of the eclipse project can be found here)I understand those typecasts are invalid, but then where in design I made mistake? Primarily I feel that I am doing it right, its just that CommonSpeedPostageProcessor needs to be parameterized with generic type parameter. But I am not able to guess exactly how do I do it. How should I generic-type-parameterize CommonSpeedPostageProcessor? Or I am simply over complicating the things?Orignal QuestionI am stuck while designing a class hierarchy for our application. Am not able to find any solution for this design constraints scenario. Design requirements(Names of classes and interfaces are over generalized to avoid unnecessary confusion due to domain specific names).Entity and EntitySet: We have organization wide Entity class and an interface identifying set of entities EntitySet which enforces basic methods needed to deal with specific type of entity we deal with in our organization. Method specific entity sets (Method1EntitySet and Method2EntitySet): Now there are two main methods to process this entity set. Each method defines class that implements EntitySet: Method1EntitySet and Method2EntitySet.Module specific interfaces and classes:Now we have different modules that are developed by different developer groups. All these modules need to use both Method1EntitySet and Method2EntitySet, however they also want these classes to have some common module specific methods. So they define interface containing module specific methods, say Module1EntitySet and Each module also defines another pair of classes Module1Method1EntitySet and Module1Method2EntitySet, both extending corresponding method specific classes (i.e. Method1EntitySet and Method2EntitySet respectively) and implementing module specific interface (i.e. Module1EntitySet). Also each module defines its own entity Module1EntityThe common logic: I want to have a class (CommonMethod1EntitySetLogic) which will contain common functionality (to be used by all modules) of processing Method1EntitySet. All modules dealing with Method1EntitySet will use this common class. Thus the methods in CommonMethod1EntitySetLogic class should return value of type which can be cast to ModuleXMethod1EntitySet or ModuleXEntitySet for any ModuleX from which the mothods are called. I felt this type of the returned values can be EntitySet or Method1EntitySet as they are not module specific. But this is not working as can be seen in below code.Note: In all above class names, I have omitted the generics type specification to not cause confusion about my design requirements. But this might have lead to incorrect question. So I am providing the code below.Codepublic class Entity { }public interface EntitySet<T extends Entity> { }public abstract class Method1EntitySet<T extends Entity> implements EntitySet<T>{}public abstract class Method2EntitySet<T extends Entity> implements EntitySet<T>{}public class Module1Entity extends Entity{}public interface Module1EntitySet extends EntitySet<Module1Entity>{}public class Module1Method1EntitySet extends Method1EntitySet<Module1Entity> implements Module1EntitySet{}public class Module1Method2EntitySet extends Method2EntitySet<Module1Entity> implements Module1EntitySet{}public class CommonMethod1EntitySetLogic {    EntitySet<Entity> method1EntitySetProcessor1()    {        Method1EntitySet<Entity> obj = null;        return obj;    }    Method1EntitySet<Entity> method1EntitySetProcessor2()    {        Method1EntitySet<Entity> obj = null;        return obj;    }}Now that all main classes given above, below is where I am getting compile time errors as specified in comments:public class Module1 {    public static void main(String[] args) {        CommonMethod1EntitySetLogic logic = new CommonMethod1EntitySetLogic();        Module1EntitySet obj = logic.method1EntitySetProcessor1(); //Type mismatch: cannot convert from EntitySet<Entity> to Module1EntitySet        Module1EntitySet obj2 = logic.method1EntitySetProcessor2(); //Type mismatch: cannot convert from Method1EntitySet<Entity> to Module1EntitySet        Module1Method1EntitySet obj3 = logic.method1EntitySetProcessor1(); //Type mismatch: cannot convert from EntitySet<Entity> to Module1Method1EntitySet        Module1Method1EntitySet obj4 = logic.method1EntitySetProcessor2(); //Type mismatch: cannot convert from Method1EntitySet<Entity> to Module1Method1EntitySet    }}I understand those typecasts are invalid, but then where in design I made mistake? Primarily I feel that I am doing it right, its just that CommonMethod1EntitySetLogic needs to be parameterized with generic type parameter. But I am not able to guess exactly how do I do it. How should I generic-type-parameterize CommonMethod1EntitySetLogic? Or I am simply over complicating the things?(The zip of the eclipse project can be found here.)
Designing generic type inheritance hierarchy
java;generics;inheritance;design patterns;design