473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inheritance - restrict methods etc.

Art
Hi,

I'd like to be able to create a class that inherits "Label". I want to
define a number of properties and methods for my new class.

The Question:
Can I prevent a user of this new class from accessing all of the properties
and methods of the base "Label" class. Some, like "Text" will need to be
available.

I'd appreciate any advice.

Art
Nov 21 '05 #1
16 1431
"Art" <Ar*@discussions.microsoft.com> schrieb:
I'd like to be able to create a class that inherits "Label". I want to
define a number of properties and methods for my new class.

The Question:
Can I prevent a user of this new class from accessing all of the
properties
and methods of the base "Label" class. Some, like "Text" will need to be
available.


I an just curious why you would want to do that. Inheritance is used to
extend an existing class, and hiding methods of a base class stands in
conflict with this principle.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
I agree on the entire 'contract' thing, but contracts CAN have fine print :)

If I want to create a new kind of car that for example can drive itself
based upon my brain waves then, given your example I should still have to
implement a steering wheel just because of the fact that I started out with
a car that had one.

Perhaps I want to start off with a Car and derive a class that is a
completely different type of car? Does this mean that I have to start out
rewriting code that reimpliments all but the 1% that I want and use that as
a base class?

Again, while I agree that in its purest form, Inheritance does imply certain
things, but I also think that it can end up getting in the way a bit.
Especially if I plan to seal my class.
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2******************@TK2MSFTNGP15.phx.gbl...
"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have tried to do this also.. People always seem to come back with that
standard 'breaks the rule of inheritance' thing that still does not sit
right with me.
If I want to inherit from a specific base class and seal my class so it
cannot be inherited from then there should not be any reason I HAVE to
implement and expose any of the base classes methods.
If you use inheritance, you are signing a contract, like when you say that
your class implements some interface. If your class is a Car which

inherits from Vehicle, you can not pretend that it has no wheels... all functions
expecting a Vehicle (of any derived class) will assume that it has wheels.
If that does not meet your needs, then you can not inherit, you must use
something else (such as encapsulation with delegation)

Inheritance can take some time to understand, but when you do, it makes
sense.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #3
A good example is a class that inheirits from another class that handles it's
own binding and doesn't want to use the standard binding yet you still have
to handle the base class binding methods, etc.

It would be nice to have an attribute or some such for hiding base class
properties and methods that the user doesn't want. If one doesn't want to do
this, then just don't use this attribute.

"Herfried K. Wagner [MVP]" wrote:
"Art" <Ar*@discussions.microsoft.com> schrieb:
I'd like to be able to create a class that inherits "Label". I want to
define a number of properties and methods for my new class.

The Question:
Can I prevent a user of this new class from accessing all of the
properties
and methods of the base "Label" class. Some, like "Text" will need to be
available.


I an just curious why you would want to do that. Inheritance is used to
extend an existing class, and hiding methods of a base class stands in
conflict with this principle.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
When I create a class that inherits form another class, I am writing my own
contract for my new class and should not be bound by a preconcieved notion of
what my new contract should look like.

"Carlos J. Quintero [.NET MVP]" wrote:
"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP10.phx.gbl...
I have tried to do this also.. People always seem to come back with that
standard 'breaks the rule of inheritance' thing that still does not sit
right with me.
If I want to inherit from a specific base class and seal my class so it
cannot be inherited from then there should not be any reason I HAVE to
implement and expose any of the base classes methods.


If you use inheritance, you are signing a contract, like when you say that
your class implements some interface. If your class is a Car which inherits
from Vehicle, you can not pretend that it has no wheels... all functions
expecting a Vehicle (of any derived class) will assume that it has wheels.
If that does not meet your needs, then you can not inherit, you must use
something else (such as encapsulation with delegation)

Inheritance can take some time to understand, but when you do, it makes
sense.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #5
Exactly.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
When I create a class that inherits form another class, I am writing my
own
contract for my new class and should not be bound by a preconcieved notion
of
what my new contract should look like.

"Carlos J. Quintero [.NET MVP]" wrote:
"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP10.phx.gbl...
>I have tried to do this also.. People always seem to come back with that
> standard 'breaks the rule of inheritance' thing that still does not sit
> right with me.
> If I want to inherit from a specific base class and seal my class so it
> cannot be inherited from then there should not be any reason I HAVE to
> implement and expose any of the base classes methods.


If you use inheritance, you are signing a contract, like when you say
that
your class implements some interface. If your class is a Car which
inherits
from Vehicle, you can not pretend that it has no wheels... all functions
expecting a Vehicle (of any derived class) will assume that it has
wheels.
If that does not meet your needs, then you can not inherit, you must use
something else (such as encapsulation with delegation)

Inheritance can take some time to understand, but when you do, it makes
sense.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #6
Dennis,

In my opinion do we see this behaviour in the standard controls as well. I
thought AFAIK that the text in the picturebox is nothing else than an extra
Tag.

In my opinion would it be nice as such a (probably shadowed property) could
be hidden.

Just my thought,

Cor
Nov 21 '05 #7
I agree with the other respondents on this point in that I do not
precisely understand why you want to hide certain properties or methods.
There are two solutions, however, to wit:
0) Move further up the inheritance chain (i.e. inherit from Control
rather than Label), or
1) Create a custom class with a private member of the type that you want
to extend. You can then write methods and properties that access the
private member. You will have to implement every method that you wish
to use, however. for a Windows Form Control, this is a lot of work, but
it can be done -- I doubt that the benefits of doing this are worth the
amount of pain that'll go into authoring the new class.

Cheers.
---
http://code.box.sk
nemo me impune lacessit

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #8
Hi Dennis,
When I create a class that inherits form another class, I am writing my
own
contract for my new class and should not be bound by a preconcieved notion
of what my new contract should look like.


No, you are not writing a new contract from scratch, you are bound by the
clauses of the old one, because you are inheriting. You can override the
behaviour of the methods of the base class, but that's all, you can not
remove them. It is like when you declare:

Class MyClass
Implements IMyInterface

You must implement each method of IMyInterface, you can not, as much as you
wish, not to implement one of them.

Inheritance is the same, but you get the implementation, for free,
automatically without writing code, which you can override writing code if
you don´t like it. That's how inheritance works in OOP languages.

The alternative, which maybe meets your needs better, is to declare a class
that does not inherit from the base class but encapsulates it instead:

Class MyClass
Private m_objMyBaseClass As New BaseClass

Public Sub f1()
m_objMyBaseClass.f1()
End Sub

' MyClass chooses not to expose f2 of BaseClass
' Public Sub f2()
' m_objMyBaseClass.f2()
' End Sub

End Class

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #9

Ray Cassick wrote:
I agree on the entire 'contract' thing, but contracts CAN have fine print :)
If I want to create a new kind of car that for example can drive itself based upon my brain waves then, given your example I should still have to implement a steering wheel just because of the fact that I started out with a car that had one.
If part of the definition of 'car' is 'has a steering wheel' then
logically anything without a steering wheel IS NOT a car and so should
not be (an instance of) a class derived from 'car'. I hate the phrase
but it's important to remember, as the books tell us, that inheritance
represents 'IS A' relationships.

Perhaps I want to start off with a Car and derive a class that is a
completely different type of car? Does this mean that I have to start out rewriting code that reimpliments all but the 1% that I want and use that as a base class?


If it's different enough that it's not actually a Car, don't derive
from Car. In your example above (like a car, but no steering wheel),
you might look for one of Car's parents to derive from -
WheeledVehicle, maybe.

--
Larry Lard
Replies to group please

Nov 21 '05 #10
"Dennis" <De****@discussions.microsoft.com> schrieb:
A good example is a class that inheirits from another class that handles
it's
own binding and doesn't want to use the standard binding yet you still
have
to handle the base class binding methods, etc.

It would be nice to have an attribute or some such for hiding base class
properties and methods that the user doesn't want. If one doesn't want to
do
this, then just don't use this attribute.


If the property is marked as 'Overridable', you can override it, mark the
overrided version as 'Browsable(False)' and throw an appropriate exception:

Untested:

<Browsable(False)> _
Public Overrides Property AutoScroll() As Boolean
Get
...
End Get
Set(ByVal Value As Boolean)
...
End Set
End Property
///

The main problem with this is that many methods are not marked as
'Overridable' although it would make sense.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #11
"Cor Ligthert" <no************@planet.nl> schrieb:
In my opinion do we see this behaviour in the standard controls as well. I
thought AFAIK that the text in the picturebox is nothing else than an
extra Tag.


Although it would sometimes make sense to hide inherited methods from the
user, removing them would not make sense because it would break the
principle of extension by inheritance. Consider a variable of type
'Control' containing a reference to a picturebox control. The 'Control'
variable will always show the 'Text' property because 'Control' has such a
property and because of inheritance all inherited controls will have such a
property too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12
Art
Hi,

FIrst, just in case it's not obvious to everyone, I'm something of a novice
at this.

Now, what is it that I'm trying to do...

I want to create my own group of controls (Base Classes), like MyTextBox.
This is for a particular group of projects I'm working on. I will add some
properties and methods to the TextBox control in order to make MyTextBox. I
will then instantiate MyTextBox as needed.

There are lots of properties and methods that get inherited when I create
MyTextBox. Many of these will not be needed -- and may confuse things. What
I ultimately want is a TextBox control that has my additional properties and
methods, but does not encourage the use of properties and methods of the base
class that do not have applicability in this project.

I understand what some of the folks are saying about inheritence being "is
a". Perhaps there's a better way to do this.

Art
Nov 21 '05 #13
Then inheritance won´t meet your needs in this case. You have to create
MyTextBox encapsulating (not inheriting) a TextBox. That's the only way that
was possible in VB6 (which lacked inheritance) and it is so:

- You create a Window Usercontrol and name it MyTextBox
- Put a TextBox control (TextBox1) in the designer surface and add code to
resize it whenever the size of the usercontrol changes or use the Anchor
property
- Add code to expose the properties and methods that you want. For example:

Public Property MyText() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal Value As String)
Me.TextBox1.Text = Value
End Set
End Property

Notice that your MyTextBox control inherits from Usercontrol (exposing its
properties) but does not inherit from TextBox (and therefore it lacks the
MaxLength property, for example).

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Art" <Ar*@discussions.microsoft.com> escribió en el mensaje
news:B5**********************************@microsof t.com...
Hi,

FIrst, just in case it's not obvious to everyone, I'm something of a
novice
at this.

Now, what is it that I'm trying to do...

I want to create my own group of controls (Base Classes), like MyTextBox.
This is for a particular group of projects I'm working on. I will add
some
properties and methods to the TextBox control in order to make MyTextBox.
I
will then instantiate MyTextBox as needed.

There are lots of properties and methods that get inherited when I create
MyTextBox. Many of these will not be needed -- and may confuse things.
What
I ultimately want is a TextBox control that has my additional properties
and
methods, but does not encourage the use of properties and methods of the
base
class that do not have applicability in this project.

I understand what some of the folks are saying about inheritence being "is
a". Perhaps there's a better way to do this.

Art

Nov 21 '05 #14
How much overhead does this method add to a control though? It seems like it
adds some additional resource overhead because you are now really including
two controls for every one that you place on a form.

How does this impact thins like tab order and focus as well?

I am not trying to say it is bad, I am just trying to get straight some
alternatives here.
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:uz**************@TK2MSFTNGP12.phx.gbl...
Then inheritance won´t meet your needs in this case. You have to create
MyTextBox encapsulating (not inheriting) a TextBox. That's the only way that was possible in VB6 (which lacked inheritance) and it is so:

- You create a Window Usercontrol and name it MyTextBox
- Put a TextBox control (TextBox1) in the designer surface and add code to
resize it whenever the size of the usercontrol changes or use the Anchor
property
- Add code to expose the properties and methods that you want. For example:
Public Property MyText() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal Value As String)
Me.TextBox1.Text = Value
End Set
End Property

Notice that your MyTextBox control inherits from Usercontrol (exposing its
properties) but does not inherit from TextBox (and therefore it lacks the
MaxLength property, for example).

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Art" <Ar*@discussions.microsoft.com> escribió en el mensaje
news:B5**********************************@microsof t.com...
Hi,

FIrst, just in case it's not obvious to everyone, I'm something of a
novice
at this.

Now, what is it that I'm trying to do...

I want to create my own group of controls (Base Classes), like MyTextBox. This is for a particular group of projects I'm working on. I will add
some
properties and methods to the TextBox control in order to make MyTextBox. I
will then instantiate MyTextBox as needed.

There are lots of properties and methods that get inherited when I create MyTextBox. Many of these will not be needed -- and may confuse things.
What
I ultimately want is a TextBox control that has my additional properties
and
methods, but does not encourage the use of properties and methods of the
base
class that do not have applicability in this project.

I understand what some of the folks are saying about inheritence being "is a". Perhaps there's a better way to do this.

Art


Nov 21 '05 #15
> How much overhead does this method add to a control though? It seems like
it
adds some additional resource overhead because you are now really
including
two controls for every one that you place on a form.
Not much really, and it is one of the 3 well known techniques to create new
controls, "Composite controls", is called. See

Windows Forms Control Development Basics
http://msdn.microsoft.com/library/de...mentbasics.asp
How does this impact thins like tab order and focus as well?


I have not tested it, but it should be explained in the docs. In your case,
with only one control, it should be easy to handle, if some handling is
needed at all.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #16
Yeah, I know all about composite controls... I just never really considered
doing it for the case of one control before...
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:eO****************@tk2msftngp13.phx.gbl...
How much overhead does this method add to a control though? It seems like
it
adds some additional resource overhead because you are now really
including
two controls for every one that you place on a form.


Not much really, and it is one of the 3 well known techniques to create
new controls, "Composite controls", is called. See

Windows Forms Control Development Basics
http://msdn.microsoft.com/library/de...mentbasics.asp
How does this impact thins like tab order and focus as well?


I have not tested it, but it should be explained in the docs. In your
case, with only one control, it should be easy to handle, if some handling
is needed at all.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

Nov 21 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
26
by: Michael A. Covington | last post by:
Suppose class X has some public properties, and class Y inherits from X. Is there a way that some of the public properties of X can be private (hidden) in Y (but still usable by the methods...
5
by: Pat Kelly via .NET 247 | last post by:
I have the following code that will not compile. It gives'A.Protected Sub SomeMethod()' is not accessible in this contextbecause it is 'Protected'. Class B can call the inherited method(SomeMethod)...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
3
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.