Quick Newbie Inheritance Question

M

Mark

Hi I was wondering if anyone could help me with the best approach for
the following.

I have an class called say BigClass with lots of bits and pieces in
it.

I currently have a data access object BigClassGetter which returns a
new populated BigClass object. Now I create a class BiggerClass which
inherits from BigClass with the addition of some other bits of info. So
what I want to do in terms of data access is to use BigClassGetter to
get the BigClass and then just add on the extra information for
BiggerClass.

My problem is BigClassGetter obviously returns BigClass so how can I
create an instance of BiggerClass from that. Sort of cast BigClass up
to BiggerClass. I was thinking of passing BigClass as a constructor to
BiggerClass and copying the info within that but I get the feeling
thats wrong.

Can anyone poing me in the right direction in terms of how to approach
the problem? Should my data access routines lie within the classes
themselves???
 
G

Guest

Create a BiggerClassGetter which inherits from BigClassGetter. seperate the
instantiation of BigClass to a seperate methods in BigClassGetter and mark it
virtual. then override it in BiggerClassGetter and instantiate a BiggerClass.
Then the inherited code will populate that.


Ciaran O'Donnell
 
M

Mark

This is exactly what I was thinking I should do however the snag I
coudlnt get round was in my override of the getter method i simply
wanted to call base.getBigClass and then do my getBiggerClass data
access logic after that to populate the additional fields.

In reality the class Im talking about is a base class Employee and and
inherited class of say Officer so Officer inherits from employee and
contains all the employee information with some additional fields on
top. I really want to use the base EmployeeGetter.Get method in the
OfficerGetter.Get override to get the base data rather than having to
replicate the dataaccess for the employee information as then Ive got
two places where I have to maintain it which seems wrong to me.

something like

public class OfficerGetter : EmployeeGetter
{
public Officer Get()
{
Employee emp = base.Get()
** Now Somehow convert this emp object to an officer object**
}
}
 
M

Mark

This is exactly what I was thinking I should do however the snag I
coudlnt get round was in my override of the getter method i simply
wanted to call base.getBigClass and then do my getBiggerClass data
access logic after that to populate the additional fields.

In reality the class Im talking about is a base class Employee and and
inherited class of say Officer so Officer inherits from employee and
contains all the employee information with some additional fields on
top. I really want to use the base EmployeeGetter.Get method in the
OfficerGetter.Get override to get the base data rather than having to
replicate the dataaccess for the employee information as then Ive got
two places where I have to maintain it which seems wrong to me.

something like

public class OfficerGetter : EmployeeGetter
{
public Officer Get()
{
Employee emp = base.Get()
** Now Somehow convert this emp object to an officer object**
}
}
 
G

Guest

Try some like this:

class EmployeeGetter
{
private object _employee;
public EmployeeGetter()
{
Create();
}
protected virtual object Create()
{
this._employee = new Employee();
}
public virtual object Get()
{
// Get the Employee data
return this_employee;
}
}

class OfficerGetter : EmployeeGetter
{
protected override object Create()
{
this._employee = new Officer();
}
public override object Get()
{
base.Get();
// Get the Officer extra data
return this._employee;
}
}

When you use this:

EmployeeGetter emGet = new EmployeeGetter();
Employee em = (Employee) emGet.Get();
OfficerGetter ofGet = new OfficerGetter();
Officer of = (Officer) ofGet.Get();

Mihaly
 
M

Mark

Mihaly, I did try to do something like this but I couldnt seem to get
it to cast up from employee to officer

object emp = new Employee();
Officer officer = (Officer)emp;

failed on invalid cast
 
M

Marc Gravell

I'm assuming that Officer : Employee?

In which case, the runtime is right. If you create an *Employee*, then it
*isn't* an Officer. If, however, you had an Officer instance, that you help
(legitimately) in an Employee variable, then you could successfully cast
that to an Officer. Because it is one.

Casting (in this context) doesn't change the object; it allows you to say
*attempt to treat this as {x}*, and will only succeed if successful.

Does that make sense? Basically, the following would work (but is
unnecessary):

Employee emp = new Officer();
Officer officer = (Officer)emp;

Marc
 
G

Guest

Sorry! Try this:

public class EmployeeGetter
{
protected object _employee;
public virtual object Get()
{
Create();
// Employee properties here
return this._employee;
}
protected virtual void Create()
{
this._employee = new Employee();
}
}

public class OfficerGetter : EmployeeGetter
{
protected override void Create()
{
this._employee = new Officer();;
}
public override object Get()
{
base.Get ();
// Officer properties here
return this._employee;
}
}

Mihaly
 
M

Mark

Marc yeah bascially that my question really because in the real world
the officer IS an employee and has all the attributes of an employee
but also has some additional attributes. So in short what I really
want to do is use the existing data access for the employee because
that will be identical and I dont want to have replicate that logic in
the officer getter class but I do want to end up with an officer object
at the end. I guess what Im asking really is how do you guys do this I
presume it must be a common scenario????
 
M

Mark

Thanks Mihaly I think I missed what you trying to do before but I see
now how this would work.
 
M

Marc Gravell

That is fine... you can use an Officer as an Employee (for your existing
data access) without any casting etc.

You need to separate *object* type from the *variable* type. The *object*
type is determined by which constructor is called. If you create an
Employee, it *is* an Employee and will always be an employee. The field /
variable, however, that is typed as Employee can actually hold either an
Exmployee or anything that derives from Employee. For instance, it will
accept an Officer (if Officer : Employee) without any casting etc.

Casting (here) just says "I am confident that this object (currently known
only to be an Employee) is actually an Officer; please let me put this into
an Officer field / parameter". If your confidence is misplaced, it will
error.

An object of type Employee simply isn't an Officer. If (as an example) you
need to change the type of an object, then either you need to hold that as a
property, or you need a different model. You could, for instance, clone the
properties from the old Employee into the new Officer record, but that is
ugly. You could have an Officer ctor that does this for you, but it would
still leave 2 objects representing (essentially) the same business
construct, and anything referencing the old Employee will have to be
manually updated to the new Officer.

Lots of problems.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top