Imagine I have a game with a base Entity class, with an init method that takes no arguments. Now I have a Wizard class, but I want to pass in 2 parameters, say speed and strength. In AS3 (and I believe Java and C#) I will not be allowed to do this – it is an “incompatible override” because the method signatures won’t match. Now I could just make an “initWizard” method instead, but then I have the problem of the init method of each class potentially having different names.
I need solutions that work in AS3, Java or C#.
Not sure I understood your question correctly. But the following compiles in Java:
Though I agree that
B#init(int,int)is not an override ofA#init(int), andB#init(int)can still be called. You can not really hide the existence ofB#init(int), at best, you could override it inBand let it throw an exception if it is called.Is it however so a big problem? The method signature capture only part of the contract of how a class must be used. If you want to shield your code a bit more, you could use a factory method to create instances of
AorBand makeinitprotected.