not inherit property

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hi, a question probably asked before, but I can't find the answers.
Base class X, classes A, B and C inherit class X.
In class A I do not want to inherit property (or function or method) P1.
Possible? How?
Thanks in advance
Frank
 
Frank,
If A does not have a P1, but B & C do, then I would recommend you reevaluate
your object model. As it really sounds like A inherits from X, while B & C
inherits from Y, where Y inherits from X and Y has a P1.

Something like:

Public MustInherit Class X

End Class

Public MustInherit Class Y

Public Sub P1()

End Sub

End Class

Public Class A : Inherits X

End Class

Public Class B : Inherits Y

End Class

Public Class C : Inherits Y

End Class

Remember when Class A inherits X, you are stating that A *is a* X, every
place an X can be used an A can be used, which means that A has to have
every thing that an X has!

Hope this helps
Jay
 
Jay,
it's a possibility. But if I need 100 props that are needed by A, B and C,
and only one of those 100 is not needed by A. Then it's more efficient to
remove that one from A. Isn't it?
Frank
 
Frank,
As I stated:Wanting to remove P1 from X is stating A *IS NOT A* X.

Which is it, is A an X or Not?

As I stated you cannot remove a member from a class, if you "removed" P1
from A, what do you expect the following code to do?

Dim anX As X
anX = New A

anX.P1

Remember that X has a P1, A inherits from X so you can assign an A object to
an X variable. Because you have an X variable you can call P1, however you
firmly insist that P1 does not exist on A. Yet the above code is calling P1
on A. What do you expect to happen? I am attempting to tell you what will
happen & why.

If the second base class causes polymorphism problems, I would consider
making P1 overridable, and override the method in A and simply throw a
NotSupportedException.

Something like:
Public Overridable Sub P1()
End Sub

Public Overrides Sub P1()
Throw New NotSupportedException()
End Sub
Then it's more efficient to
remove that one from A. Isn't it?

Introducing a second base class does not cause any "efficiency" problems.

I find it better to write "correct" programs then to worry about "what is
optimized". By "correct" I mean OO and use the correct tool for the correct
job (for example: when to add a second base class & when not to). Remember
that most programs follow the 80/20 rule (link below) that is 80% of the
execution time of your program is spent in 20% of your code. I will optimize
the 20% once that 20% has been identified & proven to be a performance
problem via profiling (see CLR Profiler in my other message).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf

Hope this helps
Jay
 
Jay,
so I guess it is not possbile to remove a property.

On the efficiency: I mean the work I have to type all the stuff into the
classes.
X with 10 properties.
A,B and C inherit from X.
But A does not need prop P1.
Meaning I have to introduce Class Y with 9 props.
Then I have to type 9 props (and methods and functions....) twice. U could
copy them. Ok, I would do that. But what about maintenance and forgetting X
and Y are almost the same and a change has to be done in both classes?
On the other hand I can have class X with 9 props and put P1 in B and C.
Thats nice for 1 prop, but what if u have dozens? And then I have 2 exactly
the same props and could forget to maintain them one of them.
I guess I will have to resort to your suggestion about
NotSupportedException.
Regards .
Frank
 
Frank said:
Jay,
so I guess it is not possbile to remove a property.

It's not very easy, no. And not particularly desirable, as Jay states. You
can take the MS approach, and just have it do nothing (or affect nothing) in
the one class which does not need the implementation.

Best Regards,

Andy
 
Frank,
On the efficiency: I mean the work I have to type all the stuff into the
There is NO efficiency problems! No duplicate typing!!

Please look closer at my original response!
X with 10 properties.

X has 9 properties

Y has 1 property, Y inherits from X
A,B and C inherit from X.

B & C inherit from Y, again Y inherits from X

A inherits from X
Meaning I have to introduce Class Y with 9 props.

No Y only needs the props that A does not have!
Then I have to type 9 props (and methods and functions....) twice. U could
copy them. Ok, I would do that. But what about maintenance and forgetting
X
There is NO cut & paste programming needed! No duplication of code, no
maintenance problems!

Remember you *CAN* have more then one base class by layering one base class
from a second base class. In your case X has the 9 common members, Y has the
1 common member. Y inherits from X to gain the other 9 common members. A
only needed the 9 inherits from X. B & C needing all ten inherit from Y
which give then all 10. There is NO duplication of code. Neat clean object
oriented design!

Something like:

Public MustInherit Class X

Public Sub P2()

End Sub

Public Sub P3()

End Sub

Public Sub P4()

End Sub

Public Sub P5()

End Sub

Public Sub P6()

End Sub

Public Sub P7()

End Sub

Public Sub P8()

End Sub

Public Sub P9()

End Sub

Public Sub P10()

End Sub

End Class

Public Class A : Inherits X

End Class

Public MustInherit Class Y

Public Sub P1()

End Sub

End Class

Public Class B : Inherits Y

End Class

Public Class C : Inherits Y

End Class


Hope this helps
Jay




Frank said:
Jay,
so I guess it is not possbile to remove a property.

On the efficiency: I mean the work I have to type all the stuff into the
classes.
X with 10 properties.
A,B and C inherit from X.
But A does not need prop P1.
Meaning I have to introduce Class Y with 9 props.
Then I have to type 9 props (and methods and functions....) twice. U could
copy them. Ok, I would do that. But what about maintenance and forgetting X
and Y are almost the same and a change has to be done in both classes?
On the other hand I can have class X with 9 props and put P1 in B and C.
Thats nice for 1 prop, but what if u have dozens? And then I have 2 exactly
the same props and could forget to maintain them one of them.
I guess I will have to resort to your suggestion about
NotSupportedException.
Regards .
Frank
<<snip>>
 
Ok, I understand what u mean. I'll look into it.
Thanks for your time
Frank
 

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

Back
Top