Inheritance

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am playing with Inheritance and want to make sure I understand it.

I have the following Classes:
*******************************************
Public Class AuthHeader:Inherits SoapHeader
Public AuthHeaderName As String
Public SessionKey As String = "Default"
End Class

Public Class ServiceTicket:Inherits AuthHeader
Public IsAuthenticated As Boolean
Public SessionKey As String = "Default from ServiceTicket"
Public Expiration As DateTime
End Class

Public Class TempClass1:Inherits ServiceTicket
Public TomsClass1Name As String
End Class
*****************************************

I assume that inheritance depends on the order of definition.

Here SessionKey is being defined in 2 classes. For TempClass1 and
ServiceTicket, Session would be "Default from ServiceTicket" since it would
have been defined second. So if I create TempClass1, it would work
something like, SessionKey would first be "Default" but then ServiceTicket
would re-define it, since it inherited it (and is higher up the chain) and
change it to "Default from ServiceTicket".

In this case, if I had:

Dim objTempClass1 As new TempClass1
Dim objServiceTicket As new ServiceTicket
Dim objAuthHeader As new AuthHeader

objTempClass1.SessionKey = "Default from ServiceTicket" '
Inherited from ServiceKey
objServiceKey.SessionKey = "Default from ServiceTicket"
objAuthHeader.SessionKey = "Default"

But if I took out the Sessionkey from the ServiceKey class that it would be:

objTempClass1.SessionKey = "Default" ' Inherited from
AuthHeader
objServiceKey.SessionKey = "Default" ' Inherited
from AuthHeader
objAuthHeader.SessionKey = "Default"

Also, I originally got this from a sample program that didn't set the
defaults, but had it defined as:
*******************************************
Public Class AuthHeader:Inherits SoapHeader
Public AuthHeaderName As String
Public SessionKey As String
End Class

Public Class ServiceTicket:Inherits AuthHeader
Public IsAuthenticated As Boolean
Public SessionKey As String
Public Expiration As DateTime
End Class

Public Class TempClass1:Inherits ServiceTicket
Public TomsClass1Name As String
End Class
*****************************************

Is there any good reason to defining SessionKey again ServiceTicket?

I assume since it always would inherit from AuthHeader, it would always get
SessionKey from the inheritance. If it wanted to set SessionKey in
ServiceTicket, it could just set it by:

SessionKey = "Default"

without needing to redefine it.

Is this correct?

Thanks,

Tom
 
yes the reason to override are more applicable to methods, properties yes..
if you want to additional functionality to than what is available in the
base class.. I also strongly recommend that any public variables you declare
and intended to use a property or have thought it must be overridden, given
them as property definitions its much cleaner that way

VJ
 
Vijay said:
yes the reason to override are more applicable to methods, properties
yes.. if you want to additional functionality to than what is available in
the base class.. I also strongly recommend that any public variables you
declare and intended to use a property or have thought it must be
overridden, given them as property definitions its much cleaner that way

I agree.

This was just a quick example of the what I was trying to play with. I just
wanted to make sure I was understanding what was happening correctly.

I wasn't sure if there was 1 variable or 2. I assume it was 1 and the last
defined one was the one I would see.

I heard from someone else that if I wanted 2 variables I would define it as
shadows (override being the default). I would still get the last defined
one, but I could also get the one in the base class by using the base class
name with the variable. Not sure why I would need that, but I am sure there
are good reasons for it. Right now I am just trying to get the basics of
inheritance down. I understand it, I just haven't build any objects that
were inherited by another object (other than the normal system ones).

Thanks,

Tom
 
Personally I will try to avoid shadows.. as much as I can.. it just causes
confusion in readability. That is what I have seen in huge projects..

Vijay
 
Back
Top