Fundamental OOP inheritance question

R

RSH

Hi,

I have been construction a sample application to further my growth in
applying OOP concepts in .Net.

My code is structured like this:

Person |-- Employee
|--- Customer

CatalogItems

Order

OrderLineItems

So my instantiation looks like this:
Dim i1 As CatalogItems = New CatalogItems(100, "Bottle", 19.95)

Dim i2 As CatalogItems = New CatalogItems(200, "Can", 10.95)

Dim i3 As CatalogItems = New CatalogItems(300, "Box", 22.95)

Dim c1 As Customer = New Customer(11111111, "Jim Williams", "123 Main St.",
"Atlanta", "GA", "30188")

Dim c2 As Customer = New Customer(12212221, "Fred Smith", "445 Fieldstone
Pkwy", "Royal Oak", "MI", "48098")

Dim no1 As Order = New Order(1000, c1, Now())

no1.AddLineItem(i1, 23)

no1.AddLineItem(i3, 12)

no1.PrintInvoice()



My question revolves around inheritance. When i create a New Order Object,
I am passing it a customer object. The dynamic nature of this is evident
when I create a new order the customer object becomes part of the of the
order. If I change the property FIRST_NAME from Jim to James for example it
progates down to the order as well. Is there anyway to freeze certain
properties in the customer object so that any changes to the FIRST_NAME
Property only affect the main class...not the sub clasess?
 
M

Marina Levit [MVP]

I don't understand what you mean by 'propagates down to the order'.

There is only one Customer object instance. It is the only thing that has
the FIRST_NAME property. There may be serveral references all pointing to
this Customer instance - but they are all pointing to the same location in
memory. It doesn't matter which reference you are using to get to this
Customer, because they are all pointing to the same thing.

So there is no propogation. Anything pointing to this one Customer will see
exactly what everything else pointing to it sees.

If you want to change the Customer variable c1, but not have the Customer of
no1 changed, then you can't pass 'c1' to the constructor of Order. You have
to create a brand new Customer object which happens to be an exact copy of
'c1', and pass that. That way, you will have 2 Customer objects in memory,
that are completely independent.
 
T

Tim Patrick

When you assign the customer to the Customer property in your order record,
you are telling it to point to the same instance, the same portion of memory,
as the original customer instance. The only way to break them is to make
a member-wise copy of the customer entry. That is, create a new customer
instance and copy properties from the original to the copy. Of course, this
is risky as it could cause data to get out of sync.

Sometimes it is useful to retain the original shipping address on an order,
even if the customer moves at a later time. In that case, you need to make
separate copies of the address and store them with the order, and not with
the customer record.
 
C

Cor Ligthert [MVP]

RSH,

The basic thing from using Objects is that an Object can be referenced from
many places, but that created object stays the same.

If I phone you and you are on another place as usuasly however have set in
your standard telephone that you are in another place than I can reach you.
That is because that you have added a reference. You stay however the same
person on whatever place you are.

Dim RSH as new Person
Dim RSHOtherplace as Person = RSH

RSH and RSHOtherplace are referencing to the same object, which is exactly
the wanted behaviour in object oriented programming.

If you don't that but are talking about another person than you do
Dim RSH as new Person
Dim Another as new Person

I hope that I answer your question in other words.

Cor
 
P

Phill W.

RSH said:
My question revolves around inheritance.
If I change the property FIRST_NAME from Jim to James for example it
progates down to the order as well.

No it doesn't. The Order object has a /reference/ to a Customer object.
If you have /another/ reference to the same Customer object and change
a value on that object, then the change is apparent in both places,
because they are both using the /same/ object.

Class Simple
Public Sub New( ByVal sName As String )
...
Property Name() As String
...
End Class

Dim p1 As New Simple( "Fred" )
Dim p2 As Simple = p1

?p1.Name
"Fred"

p2.Name = "Barney"

? P1.Name
"Barney"
Is there anyway to freeze certain properties in the customer object so
that any changes to the FIRST_NAME Property only affect the main class...
not the sub classes?

Not usually, no.

HTH,
Phill W.
 

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

Similar Threads

OOP Design Question 7
OOP Inheritance 8

Top