Inheritance. No-one seems to know this!

Tim Field

Newcomer
Joined
Aug 31, 2005
Messages
20
Hi All,

I've asked around and no-one seems to be able to answer me!...
Here's my question(s):

All in C#:
Abstract collection based on an arraylist "ab_collection.cs"
Abstract entity object which stores ID and Description "ab_entity.cs"

Real collection based on abstract collection "number_of_people.cs"
Real object based on abstract entity "person.cs"


Question 1:
If I put an add method on my abstract collection
public void Add(ab_entity entity)
{
addme(entity);
}

When I inherit the method within "number_of_people" will it loose any of my entity if I pass a "person" object to it.

Question 2:
If my "person" entity has additional storage of FirstName and Surname, can I call an undo on the base class?....

Method on my abstract entity
public void ClearAllAttributes(ab_entity entity)
{
foreach(abstract_attribute aa in entity);
{
aa.clear();
}
}

Would this method clear the FirstName and Surname or just the ID and Description that are on the abstract class?

Thanks all, Tim
 
Tim Field said:
When I inherit the method within "number_of_people" will it loose any of my entity if I pass a "person" object to it.

Person has an "is a" relationship with Entity, this means if A is a Person then A is an Entity, always. When assigning a more derived (Person is more derived than Entity, sometimes said Person inherits or is based on Entity) the compiler will attempt to find and use a lossless conversion between Person and Entity and because of the "is a" relationship there is a single cast operation which will do this.

The cast does not alter the memory layout of the Person. The cast does not create a new Entity. The cast takes the typed reference and attempts to return a new valid differently typed reference. This will succeed and you will obtain a new reference to the same original memory area which you will now access through a different Type. No data loss, no data alteration, no data multiplication or creation. The only thing that changes is a single new reference and your view of the data you have. User defined cast operations can perform data changes and interpretations but i advise you against worrying about that until you need them.

If my "person" entity has additional storage of FirstName and Surname, can I call an undo on the base class?....

Person derives from Entity therefore Person "is a" Entity is always true. This rule is not reversible. If i create a new class Pet which derives from Entity then Pet "is a"n Entity but a Pet is not a Person. An entity "may be" any of the types derived from it.

If and only if an Entity is also a Person then a cast operation to the more derived type will succeed. If Entity is not a Person then an InvalidCastException will be thrown. Because the cast operations do not change the data in the object the data in a Person is not lost zeroed or otherwise altered when you cast it to an Entity. For the same reason if you successfully cast an Entity into a Person then the data for that person remains unchanged from when you last accessed it as a Person instance.


These are reasonably fundemental language principles. If you don't understand this you're not seeing the flexibility and power of the language or the framework and you should consider doing some reading about object orientation and object polymorphism as well as the c# language docs.
 
Hey Wraith.

Thanks for that. Q1 makes sense.

Can I double check Q2 with you... you are saying my ClearAllAttributes method would work.

Cheers, Tim

BTW seems like lots of my fellow xperts at work here don't know this stuff!
 
I'd say your fellow experts aren't OOP programers (maybe expert sequential programmers? RPG? Cobol?), or they aren't experts. Yes, and Wraith implied, this is the tip of the iceburg.
 
Back
Top