OOP Inheritance Question

G

Guest

Hi

This question is based around VB.Net but could probably apply to c# or any
OOP language as well.

We found some old (ish) code in our code base and have been unable to decide
if it is pure genius or something else besides. As it stands we're leaning
towards something else but would welcome some outside comments.

The situation is.

We have a base class:

Public Class NewBase

Private _strName as String

Public Sub New
<constructor code here>
End Sub

Public ReadOnly Property Name() as String
Get
Return _strName
End Get
End Property

End Class

and we have an Interface:

Public Interface INewInterface

Public Readonly Property Name() as String

End Interface

What we have observed is a class that implements the interface and inherits
from the base class like so:

Public Class NewClass: Inherits NewBase
Implements INewInterface

Public Sub New()
MyBase.New()
End Sub

Public Shadows ReadOnly Property Name() as String Implements
INewInterface.Name
Get
Return MyBase.Name
End Get
End Property

End Class

The actual code found is more extensive but the example above indicates the
sort of code that has been written. As I said most of us here think that this
is rubbish, and fundementally wrong and amounts to nothing more then double
concentrated boiler-plating but there's always that nagging feeling that it
may just be genius and felt that an outside opinion would be helpful.

Just for the fun of it we have named it InterBase Inheritance :)

Sorry if this has been posted in the wrong group.

cheers

Rupert
 
J

Jesse Houwing

* Rupert Taylor wrote, On 31-7-2007 18:36:
Hi

This question is based around VB.Net but could probably apply to c# or any
OOP language as well.

We found some old (ish) code in our code base and have been unable to decide
if it is pure genius or something else besides. As it stands we're leaning
towards something else but would welcome some outside comments.

The situation is.

We have a base class:

Public Class NewBase

Private _strName as String

Public Sub New
<constructor code here>
End Sub

Public ReadOnly Property Name() as String
Get
Return _strName
End Get
End Property

End Class

and we have an Interface:

Public Interface INewInterface

Public Readonly Property Name() as String

End Interface

What we have observed is a class that implements the interface and inherits
from the base class like so:

Public Class NewClass: Inherits NewBase
Implements INewInterface

Public Sub New()
MyBase.New()
End Sub

Public Shadows ReadOnly Property Name() as String Implements
INewInterface.Name
Get
Return MyBase.Name
End Get
End Property

End Class

The actual code found is more extensive but the example above indicates the
sort of code that has been written. As I said most of us here think that this
is rubbish, and fundementally wrong and amounts to nothing more then double
concentrated boiler-plating but there's always that nagging feeling that it
may just be genius and felt that an outside opinion would be helpful.

I'd call it rubbish as well. Just let Newbase inherit from INewInterface
and you're done. Unless there are things you later plan to add to
NewInterface that should never get into NewBase you could separate them.
My guess is that the previous code owner doesn't really understand how
inheritance works.

For me any shadows in vb or new in a method declaration automatically
stinks :) (unless it's to work with a component that isn't under your
control).

Jesse
 
A

AlanT

We found some old (ish) code in our code base and have been unable to decide
if it is pure genius or something else besides.


At first glance I'd pretty much have to go with the other comments
that it seems to be an egregious example of using the shadows/new
facility but it hard to be definitive when examining just these
(simplified) classes in isolation.
Whether or not this setup is good/useful depends upon the other
classes in the system.

NewBase
^
|
NewClass implements INewInterface


Do you have any other classes inheriting from Base which do not
implement INewInterface

e.g.

NewBase
^
|
SomeOtherClass


SomeOtherClass inherits the Name property but as it does not implement
INewInterface it cannot be access through INewInterface.


For the above example it seems strange to have the INewInterface only
on the dervied class when the base class already implements ALL of the
INewInterface. As mentioned, just place it on the NewBase class.

However, I can see situations where we want an interface on the
derived class which implements lots of things, some of which are
already defined in the base class.

e.g.

INewInterface
Name
Address
Age
Height


In VB.Net we would need to shadow the Name property because it already
exists in the base class (AFAIK, in C# we do not need to 'new' the
property as implicit implementation of the interface will pick up the
Name definition from the base class) but we need to place this
interface on the NewClass because the base class does not have and
Address, Age or Height)

HTH,
Alan.
 

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