473,408 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

inheritance and instantiation

Hi

I have a
class Person : IPerson

and a
class Employee : Person, IEmployee

and an
interface IEmployee : IPerson
Say I have a method like

IPerson GetPerson(long id)
{
return new Person();
}

How do I turn the "Person" object into an "Employee" object?

Do I need to instantiate a new Employee and copy the fields from the
Person object manually (and add the extra data an Employee holds)?

I was hoping I could do something like:
IEmployee employee = (IEmployee)person;
employee.Company = "my co";

but I soon found out that won't do.
Thanks,
Peter
Sep 17 '08 #1
3 1308
Do I need to instantiate a new Employee and copy the fields from the
Person object manually (and add the extra data an Employee holds)?
Yes. Casting simply tweaks the local variable - it doesn't change the
object. If the object is a Person, it is still a Person regardless of
how you reference it. You cannot change an object's type after
creation. Note that if the GetPerson *returned* an Employee instance
(still returning as IPerson), then the cast would succeed.

Marc
Sep 17 '08 #2
IPerson GetPerson(long id)
{

Presumably the ID is used to retrieve the details from some data source.
(pseudo code)

if (ID is an employee) {
return new Employee(ID, Name, SSN etc);
}
if (ID is a manager) {
return new Manager(ID, Name, SSN etc);
}

....and so on. All are derived from Person and as such implement IPerson. A
simple yet classic Factory. Ideally you would not use the if's as I have
done, this will overtime become quite messy. Instead create something
generic where the type (Manager, employee etc) is also sourced for the data
source.

}

See System.Activator.CreateInstance()

http://en.wikipedia.org/wiki/Factory_method_pattern
Sep 17 '08 #3
As an aside, Clint's example essentially describes a "discriminator".
Many ORM tools (for plumbing your data-access code) will do this for
you. For example, LINQ-to-SQL supports a discriminator property: you
might have a column that is "E", "M" or "P" (for employee, manager and
person) - when fetching records it will automatically create the right
type of object to represent the row you fetched, and when inserting a
record it will automatically add the correct "E"/"M"/"P" value.

Marc
Sep 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
5
by: Dart | last post by:
What are the major advantages of inheritance of a class of no virtual functions, promoting code re-use? Thanks!
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
9
by: vidalsasoon | last post by:
Ok, this is the structure of my classes. " class Global " | " ------------------------------------------- " | ...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
14
by: petermichaux | last post by:
Hi, Hopefully the group doesn't mind an(other) inheritance question. Maybe the prototype inheritance style is starting to become a low dim light in my brain...probably not yet. ---- If I...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
8
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.