Newbie: confused about inheritance / context

  • Thread starter Thread starter EvelynWilde
  • Start date Start date
E

EvelynWilde

Just when I thought I'd got my head around inheritance, I'm confused -
here's a quasi-scenario:

I have an object representing a Department in my shop. It's the
sausages department, and has properties (DepartmentName, FloorArea,
Manager etc). I also have an object representing a Product (SKU, Price
etc.).

I've set up my Product to inherit from my Department. This seems
logical: all Products 'belong' in a Deparment. I've written a
GetProduct method on the Department, which returns a new Product.

While I can happily access my Department methods through my Product,
they don't return anything. e.g. Product.DepartmentName is empty - it
can be set, but will be 'disconnected' from the Department, it seems.
So I'm concluding that inheritance inherits the methods and
properties, but not the context.

So I suppose the question is: what's the best way to do this sort of
thing? How should my Product best inherit a specific instance of my
Department (and thus all its associated properties), rather than the
generic object? Is there an elegant way of doing it, or do I have to
shuffle lots of values about?

Thanks, A
 
I've set up my Product to inherit from my Department. This seems
logical: all Products 'belong' in a Deparment. I've written a
GetProduct method on the Department, which returns a new Product.

They are contained within a department but they're *not* a department, the
HasA and IsA relationship applies here. The statement "Product IsA
Department" is false so, simplistically, having Product inherit Department
is wrong. However the statement "Department HasA Product" is certainly true,
suggesting containment is the correct representation.
So I suppose the question is: what's the best way to do this sort of
thing? How should my Product best inherit a specific instance of my
Department (and thus all its associated properties), rather than the
generic object? Is there an elegant way of doing it, or do I have to
shuffle lots of values about?

I would suggest Product has a reference to a containing Department and
Department has a collection of Product's. Allowing you to obtain a
department from a product with a property and you ask the department for
products.

n!
 
Hi Evelyn,
IMHO you are wrong.
I've set up my Product to inherit from my Department. This seems
logical: all Products 'belong' in a Deparment.

Inheritance is *IS* relation. A product IS NOT a department. So there is no
way to use inheritance.

If a Department HAS something means your Department objects should keep
reference to that *something* or collection of *somethings*. *Something*
objects may keep reference back to the Departments as well if it's
necessary.
 
Back
Top