[RESOLVED] Same interface, base and derived class.
I'm struggling here.. I know I can disable the warning manually. However I'm not sure if this would be the right way to go about this.
Here's my interface..
vbnet Code:
Public Interface IPopulatingClassBase
Property Identity() As Int64
ReadOnly Property Dirty() As Boolean
Sub Populate_Class(ByVal RecordID As Int64)
Sub Save_Class()
Sub Reset_Class()
End Interface
Here's my parent class...
vbnet Code:
Public Class Parent
Implements IPopulatingClassBase
Public Property Identity() As Int64 _
Implements IPopulatingClassBase.Identity
'...
End Property
Private Dirty() As Boolean = {False, False}
Public ReadOnly Property Dirty() As Boolean _
Implements IPopulatingClassBase.Dirty
'...
End Property
Friend Sub Populate_Class(ByVal ParentID As Int64) _
Implements IPopulatingClassBase.Populate_Class
'...
End Sub
Friend Sub Save_Class() _
Implements IPopulatingClassBase.Save_Class
'...
End Sub
Public Sub Reset_Class() _
Implements IPopulatingClassBase.Reset_Class
'...
End Sub
End Class
Here's my derived class..
vbnet Code:
Public Class Derived
Inherits Parent
Implements IPopulatingClassBase
Public Property Identity() As Int64 _
Implements IPopulatingClassBase.Identity
'...
End Property
Private Dirty() As Boolean = {False, False}
Public ReadOnly Property Dirty() As Boolean _
Implements IPopulatingClassBase.Dirty
'...
End Property
Friend Shadows Sub Populate_Class(ByVal DerivedID As Int64) _
Implements IPopulatingClassBase.Populate_Class
'...
End Sub
Friend Shadows Sub Save_Class() _
Implements IPopulatingClassBase.Save_Class
'...
End Sub
Public Shadows Sub Reset_Class() _
Implements IPopulatingClassBase.Reset_Class
'...
End Sub
End Class
I obviously get the warnings in the derived class, but I feel this should be allowed. I've read vb.net doesn't whereas c# does. Is there a way around this or should I just create separate interfaces? (Which I feel defeats the entire purpose of an interface.)
PS In my example think of it as classes that populate with data from a database. The parent class would be an organization, and the derived class a location. Both populate and have similar functionality. The only difference is locations are an entity of an organization(the data record for a location constraints it to an organization) upon loading location data I also populate the organization class. This is a real example of what my project does.. so having said I just want to know how I can get around this warning, and if not would it have any consequences if I left it as-is.
Thanks in advance
David
Re: Same interface, base and derived class.
you shouldn't be inheriting a class that also implement the interface you're trying to implement.... one or the other....
your base parent class as property called Identity... because it implemented it from the interface.
Now your class ALSO implements it... AND inherits from a class that already implemented Identity... in essence, you have TWO properties Identity... that just doesn't make sense.
you can implement an interface AND inherit from a class at the same time. That's not the problem. The problem is you inherited from a class that implemented the same interface you're trying to implement again.
If I were to call DerivedClass.Populate_Class.... which one should it call? the inherited one? or the implemented one?
-tg
Re: Same interface, base and derived class.
I agree with techgnome here. If you're wanting to be able to redefine the base class function then declare it as Overridable.
Re: Same interface, base and derived class.
I would suggest considering making the base class a pure virtual that doesn't implement the interface and can't be instantiated. My reasoning is that you implemented the interface in the base class because you felt that an instance of the base class would be needed at times. However, you have run into a problem with the base and the derived both implementing the interface. Therefore, you can most readily avoid having the base class need to implement the interface by removing the possibility of the base class ever being instantiated...conceptually (you can always prevent it physically, you just want to remove the perception of the instance ever being needed). Any situation where you would currently use an instance of the base class can be alterered to a situation where you use an instance of a derived class if you make a derived class that implements the interface but otherwise provides only the base class functionality. You would then be able to remove the interface from the base class and mark it Must Override.
Re: Same interface, base and derived class.
Quote:
Originally Posted by
techgnome
you shouldn't be inheriting a class that also implement the interface you're trying to implement.... one or the other....
your base parent class as property called Identity... because it implemented it from the interface.
Now your class ALSO implements it... AND inherits from a class that already implemented Identity... in essence, you have TWO properties Identity... that just doesn't make sense.
you can implement an interface AND inherit from a class at the same time. That's not the problem. The problem is you inherited from a class that implemented the same interface you're trying to implement again.
If I were to call DerivedClass.Populate_Class.... which one should it call? the inherited one? or the implemented one?
-tg
Well it does make sense, an organization record has an Identity as does a location. Both have identities. They're not necessarily both called Identity, they're actually OrganizationID and LocationID. I should have demonstrated this in my example, but both are considered to be an identity of that class.
The problem is an organization and location class act as the same, they populate with data. But a location is constricted to an organization therefore inherits the organization class. So when I call to populate the location class, that method in turn calls it's base populating method.
@MattP These methods do shadow when they inherit the parent class. I did try the "correct" method of overriding but I was getting some odd behavior, pretty much the opposite of what I wanted so I stuck with shadows for now until I can figure it out.
PS I'll demonstrate the shadows in the original post, forgot I left that out.
Re: Same interface, base and derived class.
Just because two objects have similar properties (and thus can implement the same interfaces) doesn't mean they should.
Indeed, your example of 'Organization' and 'location' do not imply they have a parent child relationship, and so one shouldn't inherit from one or the other.
It sounds like that you should have an 'Entity' class, which implements your interface, and the Organization and Location objects inherit from the Entity object.
Re: Same interface, base and derived class.
Actually you guys were all correct.
It took me some time to get a grip on, but before I was trying to fix what wasn't mine, so with how it was structured made it difficult to suddenly implement interfaces. Thankfully, I began making a project in my spare time, a movie database library with GUI. So by starting at the beginning and structuring the different classes in a more appropriate way. I was able to use interfaces as I had originally hoped, and in the ways instructed here. A little more simple though than the project I'm working on at work but none-the-less, everything looks and works beautifully.
Also sorry it took me so long to work, busy at work. Thanks again guys!