If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Best way for creating a concrete class?

So I have a base class which has protected properties and also implements an interface.
The method in the interface, its signature has been changed in the base class to be abstract as I want the children/inheritors override/implement this method.

So now I will have a range of classes, lets call them:

Baby,
Toddler,
Child,
Teenager

All this inherit from "HumanBase", which implements an interface as explained above.

Now, I need to have some factory of some kind where it will return me the correct type of object but as an Interface i.e:

how would I set all the base properties etc... from the HumanBase from this factory type pattern? I know you can only access protected properties from the base class itself and also the children that inherit from the base class.
What I mean is, should each of the classes (baby, toddler etc...) have a constructor which takes in the parameters and then in the constructor let it set itself up?

MVP 2007-2010 any chance of a regain?
Professional Software Developer and Infrastructure Engineer.

Re: Best way for creating a concrete class?

I read your post and now am confused. Are you attempting to inject (pardon the terminology) a method in your base class and want this method will be available to the children derived from that base class?

Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'save a blobFileStreamDataTable To Text Filemy blog

Re: Best way for creating a concrete class?

Keep in mind that an abstract class is different from an interface. An interface serves as a contract stating that an implementing object has to implement the "contract" properties and methods. An abstract can force an inheriting object to override abstract properties and methods, but can also contain functionality. I know sometimes it makes sense to use a base to refactor common methods, while you would prefer to leverage the flexibility of basing object operations on the interfaces. In this case, you could implement the interface with the abstract and the concrete objects. Since the interface gets implemented with both, it's contract is understood by the concrete objects (Child, etc) without having to reimplement them. This is because the concrete objects will inherit from the base that already implemented them. See attached class diagram.

As for the coding.... This is what I did:

also... note how in the Toddler and Baby classes I implemented the constructors to initialize the base property values. I did it two different ways.