Question

I have been struggling with the abstraction that lies between two classes when it comes to the issue of Inheritance or Composition...the IS-A vs HAS-A relationship between classes like LoginManager and AuthenticateManager.

The way I see it LoginManager can be either and can sit comfortably in a IS-A place with AuthenticateManager, inheriting it as a superclass (ie Class LoginManager extends AuthenticateManager) OR just declare AuthenticateManager's objects as members of its own class which would imply Composition, so i guess it just comes down to a thing of experience and the proper knowledge of the OOP paradigm. So please, can anyone help explain what a proper relationship would be between these classes?

PS: Please moderator don't close this topic as being inconsistent with the sites question asking principles.

Thanks.

Was it helpful?

Solution

It depends on your needs. Inheritance provides you with access to protected members of a superclass whereas composition does not. If there are class members that should be available only to a derived class then it does not make sense to make them properties and access them with composition because every other object could access them as well. Otherwise it is about what you and your team prefer.

EDIT:

No rules are so strict as you think they are. Design patterns, for instance, are just templates and you don't need to strictly follow them. You can't blindly follow rules, no one will credit you for this. It's much better to have OOP in mind but write your code so that you can clearly explain what have you done and why.

Since I don't know design of your two classes, I can't really tell you what are your needs. This statement (already mentioned) should be enough to point you in the right direction.

Inheritance provides you with access to protected members of a superclass whereas composition does not.

OTHER TIPS

Why not consider something that is easier to grasp?

I have a father - I inherit his genes (some good some bad?) But hey.

He had a kidney. His own and that made his composition.

I have my own kidney - my own and that makes my composition.

So

IS-A - I IS-A child of my father HAve-A - I hAVE-A kidney

My Kidney works differently to my fathers But I have blue eyes that I have inherited from my father

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top