Discussion:
overriding virtual method has lower visibility than base class...
(too old to reply)
peter
2005-10-26 04:31:39 UTC
Permalink
I have a protected virtual method abc in Unit A and in the inherited unit B,
the method abc is overriden with visibility priviate, then when I compile, I
see the Hint as "Overriding virtual method ... has lower visibility
(private) than base class ... (protected), what is the problem of having an
overriden method with visibiltiy lower than the base class?
Rob Kennedy
2005-10-26 05:20:00 UTC
Permalink
Post by peter
I have a protected virtual method abc in Unit A and in the inherited
unit B, the method abc is overriden with visibility priviate, then
when I compile, I see the Hint as "Overriding virtual method ... has
lower visibility (private) than base class ... (protected), what is
the problem of having an overriden method with visibiltiy lower than
the base class?
There's no problem, really, which is why it's just a hint. The compiler
is telling you that although you've declared the descendant method as
private, the method is still protected because it's protected in the
base class. The compiler is pointing out to you that the method doesn't
really have the visibility you told it to have. Whether that's a problem
is up to you to decide, just as it's your choice whether an unused
variable declaration or an unused variable assignment are problems.
--
Rob
peter
2005-10-26 05:55:36 UTC
Permalink
"the method is still protected because it's protected in the
base class. ..."
Oh, the method is still protected? which means if I inherited unit B to unit
C, then unit C can override method abc in Unit B?
Post by Rob Kennedy
Post by peter
I have a protected virtual method abc in Unit A and in the inherited
unit B, the method abc is overriden with visibility priviate, then
when I compile, I see the Hint as "Overriding virtual method ... has
lower visibility (private) than base class ... (protected), what is
the problem of having an overriden method with visibiltiy lower than
the base class?
There's no problem, really, which is why it's just a hint. The compiler
is telling you that although you've declared the descendant method as
private, the method is still protected because it's protected in the
base class. The compiler is pointing out to you that the method doesn't
really have the visibility you told it to have. Whether that's a problem
is up to you to decide, just as it's your choice whether an unused
variable declaration or an unused variable assignment are problems.
--
Rob
OBones
2005-10-26 07:25:34 UTC
Permalink
Post by peter
"the method is still protected because it's protected in the
base class. ..."
Oh, the method is still protected? which means if I inherited unit B to unit
C, then unit C can override method abc in Unit B?
Absolutely, because it was declared as protected in class A.
peter
2005-10-26 07:53:16 UTC
Permalink
Sorry for my foolish, if now assume unit B has declare public visibility of
method abc, then is the method really public or is still protected like unit
A?
in fact, I know java only allow widen visibility along inheritance tree, is
this apply to Delphi?
any online material teaching this matter?

Thx!!
Post by OBones
Post by peter
"the method is still protected because it's protected in the
base class. ..."
Oh, the method is still protected? which means if I inherited unit B to
unit C, then unit C can override method abc in Unit B?
Absolutely, because it was declared as protected in class A.
Jim Cooper
2005-10-26 10:14:23 UTC
Permalink
Post by peter
in fact, I know java only allow widen visibility along inheritance
tree, is this apply to Delphi?
Yes
In fact, you do not have to override the method (or property) in a descendant
class in order to promote it's visibility. You only need to redeclare it at the
new level (ie doing that in the interface section is sufficient, no code is
required in the implementation section)


Cheers,
Jim Cooper

__________________________________________

Jim Cooper ***@tabdee.ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi to your Palm
__________________________________________
unknown
2005-10-27 01:01:52 UTC
Permalink
Post by Jim Cooper
In fact, you do not have to override the method (or property) in a descendant
class in order to promote it's visibility. You only need to redeclare it at the
new level (ie doing that in the interface section is sufficient, no code is
required in the implementation section)
I'm pretty sure that this is only true for properties.
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Jim Cooper
2005-10-27 02:06:52 UTC
Permalink
Post by unknown
I'm pretty sure that this is only true for properties.
Yes, I'm sure you're right, thinking about it.




Cheers,
Jim Cooper

__________________________________________

Jim Cooper ***@tabdee.ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi to your Palm
__________________________________________
Jim Cooper
2005-10-26 10:12:31 UTC
Permalink
Post by peter
Sorry for my foolish, if now assume unit B has declare public visibility of
method abc, then is the method really public or is still protected like unit
A?
Public in B and descendants of B
Post by peter
in fact, I know java only allow widen visibility along inheritance tree, is
this apply to Delphi?
Yes

Cheers,
Jim Cooper

__________________________________________

Jim Cooper ***@tabdee.ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi to your Palm
__________________________________________
Wayne Niddery [TeamB]
2005-10-26 15:42:56 UTC
Permalink
Post by peter
Sorry for my foolish, if now assume unit B has declare public
visibility of method abc, then is the method really public or is
still protected like unit A?
in fact, I know java only allow widen visibility along inheritance
tree, is this apply to Delphi?
Correct, you can only *promote* visibility.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"Nature abhors the vacuum tube." - J.R. Pierce, Bell Labs engineer who
coined the term 'transistor'
Rob Kennedy
2005-10-27 04:41:27 UTC
Permalink
Post by peter
Sorry for my foolish, if now assume unit B has declare public visibility of
method abc, then is the method really public or is still protected like unit
A?
in fact, I know java only allow widen visibility along inheritance tree, is
this apply to Delphi?
any online material teaching this matter?
In the Delphi 5 help, the "Visibility of class members" topic says this
in the final paragraph:

"You can increase the visibility of a member in a descendant class by
redeclaring it, but you cannot decrease its visibility."
--
Rob
Loading...