Inheritance question

R

RSH

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 make a list class that will store the objects. My
question is how do I go about creating the list class...I am assuming it
should be a standalone class that uses an arraylist to store the objects.
If I go that route how do I instantiate the objects so they get added to the
list of objects, and how do I know what type they are when iterating through
the list of objects? Or am I way off base (Pun Intended)?





public partial class Form1 : Form
{
private Child1 c1 = new Child1();
private ChildListClass clc1 = new ChildListClass();
private clc1.add(c1); << is this the correct implementation? Or do I
want to somehow make this part of the Base class and access it through a
static arraylist?
}

public Abstract Class BaseClass
{
}

public Sealed Class Child1
{
}

public Sealed Class Child2
{
}

public Class ChildListClass
{
}

Thanks!
Ron
 
P

Peter Duniho

RSH said:
I have an instance where I have a Base Abstract Class, and 4 Derived
classes. I now need to make a list class that will store the objects. My
question is how do I go about creating the list class...I am assuming it
should be a standalone class that uses an arraylist to store the objects.
If I go that route how do I instantiate the objects so they get added to
the list of objects, and how do I know what type they are when iterating
through the list of objects? Or am I way off base (Pun Intended)?

Not enough information, IMHO.

For example, what is in your ChildListClass that you want to do that
prevents you from using (for example) a generic List<T> class or a
Collection of some sort? Do you actually want your derived instances
automatically added to the list? That's not what your example code appears
to do.

As far as getting the type of the instances, you can use the GetType()
method on any object to get its type. Compare it to a specific type if you
want to know whether that object is in fact that type. Alternatively, you
can use the "is" and "as" keywords to compare types or cast types
respectively. They are slightly different, in that if a conversion from one
type to another exists, they will treat two things as the same type. If you
know that no conversion to your target type exists for other objects, or if
having such a conversion is okay, then that might be the way to go.

Pete
 
R

RSH

Peter,

Thanks for the reply. Sorry about that.

I do want to have the objects to be added to the list on instantiation...I
just didn't know how to represent that.

Thanks,
Ron
 
J

Jon Skeet [C# MVP]

RSH said:
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 make a list class that will store the objects. My
question is how do I go about creating the list class...I am assuming it
should be a standalone class that uses an arraylist to store the objects.
If I go that route how do I instantiate the objects so they get added to the
list of objects, and how do I know what type they are when iterating through
the list of objects? Or am I way off base (Pun Intended)?

Do you actually need a separate class just for the list? Any reason you
can't use an ArrayList (or a List<T> if you've got .NET 2.0) directly?
What else is the list class going to do?

When you iterate through the list of objects, why do you need to know
the actual type? If possible, put abstract or virtual methods in the
base class, and then you can just call those methods (after casting to
BaseClass if you're not using generics) and let polymorphism do the
work for you.
 
P

Peter Duniho

RSH said:
Peter,

Thanks for the reply. Sorry about that.

I do want to have the objects to be added to the list on instantiation...I
just didn't know how to represent that.

There are a number of ways to implement something like that. Without
knowing more about your specific intent, preferences, etc. I can't offer
advice on a particular implementation to choose. However, here's one
example of doing what you want:

class BaseClass
{
static private List<BaseClass> _grbcInstances;

public BaseClass()
{
_grbcInstances.Add(this);
}

public void Remove()
{
_grbcInstances.Remove(this);
}
}

The BaseClass can then provide methods for manipulating, searching, etc. the
list of objects. Note that you need to provide an explicit "Remove" method
to ensure that objects are actually released when you want them to be. This
puts the onus on you to figure out when you're actually done with an object,
at least as it relates to the master list of objects. I have seen mentions
of something called a "weak reference" that may or may not help you around
this issue; I don't know enough about weak references to offer you advice on
that.

Finally, I agree with Jon's statement about the possibility of using virtual
methods to deal with per-type behaviors. It's unusual to need to know the
actual type of an object and while I took the shortcut of simply answering
the direct question you asked, I agree that it's more likely that whatever
reason you think you need the type for, that can be more properly addressed
using virtual methods.

Pete
 
P

Peter Duniho

Peter Duniho said:
class BaseClass
{
static private List<BaseClass> _grbcInstances;

public BaseClass()
{
_grbcInstances.Add(this);
}

public void Remove()
{
_grbcInstances.Remove(this);
}
}

Of course, I left out the initializing of _grbcInstances. Sorry. But you'd
figure that out the first time you tried to run that code. :) Hopefully
it's not too hard to figure out where to add "= new List<BaseClass>()".
 
R

RSH

Thanks guys!

I was simply trying to get a better understanding of working with
inheritance.

I guess I did need to be a lot clearer on intent. Lets assume that my base
class is a Person Class. The derived classes are Employee,Customer,Vendor.
I was attempting to create a list of Persons which I figured would probably
be a static collection in the base class. But after thinking about it a bit
more would it make more sense to have a static collection in each derived
class that holds only objects of the appropriate type?

I am trying to learn inheritance so i dont really know the questions to ask.
I am just trying to sort through handling a one to many relationship when
inheritance is involved.

Ron
 
M

Michael Weber

RSH said:
Hi,

I am working on some general OOP constructs and I was wondering if I could
get some guidance.

GRASP - General Responsibility Assignment Software Patterns:
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)
I have an instance where I have a Base Abstract Class, and 4 Derived
classes. I now need to make a list class that will store the objects. My
question is how do I go about creating the list class...I am assuming it
should be a standalone class that uses an arraylist to store the objects.
If I go that route how do I instantiate the objects so they get added to the
list of objects, and how do I know what type they are when iterating through
the list of objects? Or am I way off base (Pun Intended)?

[snip]

A Container-class might be useful.
It would be responsibel for adding/removing/(searching for) the right types,
keeping the different types apart
( seperat collections ).

For creating classes a Controller-class might come in handy.
It would be responsible for checking data from UI, instantiate objects,
using the Container-class,
updating UI.
Or perhaps some sort of creational design pattern:
http://en.wikipedia.org/wiki/Creational_pattern
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

Basically the 2 classes are assigning/"dividing and conquering"
responsibilities not well suited
in the Child-classes and really having nothing to do with the Child-clases
(as far as I can see).
And perhaps that is what your assumption about a standalone class is all
about ?


Best Regards,
Michael Weber
 
P

Peter Duniho

RSH said:
I guess I did need to be a lot clearer on intent. Lets assume that my
base class is a Person Class. The derived classes are
Employee,Customer,Vendor.
I was attempting to create a list of Persons which I figured would
probably be a static collection in the base class. But after thinking
about it a bit more would it make more sense to have a static collection
in each derived class that holds only objects of the appropriate type?

Well, that depends on what you expect to do with the list. If it's
important to you for each list to contain only instances of a particular
type, then it probably does make more sense for each list to be in each
derived class instead of the base class.

There's no one "right way" to do it. It really just depends on what you
need from your data structures.
I am trying to learn inheritance so i dont really know the questions to
ask. I am just trying to sort through handling a one to many relationship
when inheritance is involved.

Well, IMHO the question you asked is a bit on the advanced side relative to
the issue of learning how inheritance works. Having a list of instances
isn't in any way required for an inheritance-based design to work, so adding
that as part of your criteria when simply trying to learn how inheritance
works over-complicates things.

It's sort of like talking about how adding an engine to a bicycle might be
done, when what you're trying to learn about is the stability of two-wheeled
vehicles. :)

Pete
 

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