Inheritance Scoping

J

Julie

Hi Everyone,
I was wondering if there is a way to hide a public property (change it to
private) on a derived class? In other words, if i have a base class that has
a public property, and i create a new class that inherits from this class,
can i change that property to be private?

Thanks in advance.
 
P

Patrice

It would go against inheritance (i.e. a derived class must expose all what
its base class exposes). It looks like you have some kind of design issue
here in your class hierarchy.

Hard to tell without knowing what you are trying to do but you could perhaps
:
- have a base class
- inherits from this base class and expose this property (it will replace
the base class you currently have)
- inherits from this base class and don't expose this property

Or perhaps is this public because you are missing the protected keyword ?
Your best bet will be likely to give some details about what you are trying
to do...
 
F

Family Tree Mike

Julie said:
Hi Everyone,
I was wondering if there is a way to hide a public property (change it to
private) on a derived class? In other words, if i have a base class that has
a public property, and i create a new class that inherits from this class,
can i change that property to be private?

Thanks in advance.

I agree with Patrice, that we need to understand what you really hope to
achieve.

You could do the following, but it likley doesn't achieve your goal.

Public Class A
Protected _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
Public Class B
Inherits A
Private Overloads Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
Sub Main()
Dim bbb As New B
Dim aaa As New A
aaa.Name = "Mike"
bbb.Name = "Fred"
End Sub

You will find that in neither case is he "Name" property in class B used
within main. In the case of bbb.Name, the object is using the class A
property.

Mike
 
B

Brian Gideon

Hi Everyone,
I was wondering if there is a way to hide a public property (change it to
private) on a derived class? In other words, if i have a base class that has
a public property, and i create a new class that inherits from this class,
can i change that property to be private?

Thanks in advance.

No, that would violate the interchangableness of the base-derived
pair. See the Liskov Sustitution Principal for more information.
 
C

Cor Ligthert[MVP]

No,

You see this for instance with the controls, the picturebox shows a
background property, but it is simply shadowed. It does nothing.

Cor
 
H

Herfried K. Wagner [MVP]

Julie said:
I was wondering if there is a way to hide a public property (change it to
private) on a derived class? In other words, if i have a base class that
has a public property, and i create a new class that inherits from this
class, can i change that property to be private?

No, as this would break polymorphy.

Consider a class 'B' derived from 'A'. 'A' has a public method named 'S':

\\\
Dim a As A = New B()
a.S()
///
 

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