Skip to main content
added 241 characters in body
Source Link
klaar
  • 121
  • 9

When I implement a base class that requires extra initialisation information or links to other objects before it becomes useful, I tend to make that base class abstract, then define several abstract methods in that class that are used in the flow of the base class (like abstract Collection<Information> get_extra_information(args); and abstract Collection<OtherObjects> get_other_objects(args); which by contract of inheritance must be implemented by the concrete class, forcing the user of that base class to supply all the things required by the base class.

Thus when I implement the base class, I immediately and explicitly know what I have to write to make the base class behave correctly, since I just need to implement the abstract methods and that's it.

EDIT: to clarify, this is almost the same as providing arguments to the constructor of the base class, but the abstract method implementations allow the implementation to process the arguments passed to the abstract method calls. Or even in the case of no arguments being used, it can still be useful if the return value of the abstract method depended on a state that you can define in the method body, which isn't possible when passing the variable as an argument to the constructor. Granted you can still pass an argument that will have dynamic behaviour based on the same principle if you'd rather use composition over inheritance.

When I implement a base class that requires extra initialisation information or links to other objects before it becomes useful, I tend to make that base class abstract, then define several abstract methods in that class that are used in the flow of the base class (like abstract Collection<Information> get_extra_information(); and abstract Collection<OtherObjects> get_other_objects(); which by contract of inheritance must be implemented by the concrete class, forcing the user of that base class to supply all the things required by the base class.

Thus when I implement the base class, I immediately and explicitly know what I have to write to make the base class behave correctly, since I just need to implement the abstract methods and that's it.

When I implement a base class that requires extra initialisation information or links to other objects before it becomes useful, I tend to make that base class abstract, then define several abstract methods in that class that are used in the flow of the base class (like abstract Collection<Information> get_extra_information(args); and abstract Collection<OtherObjects> get_other_objects(args); which by contract of inheritance must be implemented by the concrete class, forcing the user of that base class to supply all the things required by the base class.

Thus when I implement the base class, I immediately and explicitly know what I have to write to make the base class behave correctly, since I just need to implement the abstract methods and that's it.

EDIT: to clarify, this is almost the same as providing arguments to the constructor of the base class, but the abstract method implementations allow the implementation to process the arguments passed to the abstract method calls. Or even in the case of no arguments being used, it can still be useful if the return value of the abstract method depended on a state that you can define in the method body, which isn't possible when passing the variable as an argument to the constructor. Granted you can still pass an argument that will have dynamic behaviour based on the same principle if you'd rather use composition over inheritance.

Source Link
klaar
  • 121
  • 9

When I implement a base class that requires extra initialisation information or links to other objects before it becomes useful, I tend to make that base class abstract, then define several abstract methods in that class that are used in the flow of the base class (like abstract Collection<Information> get_extra_information(); and abstract Collection<OtherObjects> get_other_objects(); which by contract of inheritance must be implemented by the concrete class, forcing the user of that base class to supply all the things required by the base class.

Thus when I implement the base class, I immediately and explicitly know what I have to write to make the base class behave correctly, since I just need to implement the abstract methods and that's it.