If one declares class2.method1 as virtual, can you still call inherited method1; in it? The compiler warns me that it hides the ancestral method.
A little help would be appreciated.
If you declare
class2.method1 as
virtual you're starting a
new method chain, which is why the compiler will warn you that
class1.method1 is hidden. A method declared with
override is implicitly
virtual as well (assuming there is anything to override in the first place, otherwise you're not allowed to use
override anyway and must use
virtual instead). Use
reintroduce in addition to
virtual to really go through with the new method chain (and thus suppressing the warning).
In any case you can still call
inherited method1 from
class2.method1 to indeed call
class1.method1.
The difference will be if you have a variable of type
class1 and call
method1 then in case of a new method chain the compiler will call
class1.method1 (cause that isn't overridden) and in case of a continued method chain it will call
class2.method1 or
class3.method1 depending on what you instantiated it as.
Class3.Method1 should call inherited method1 (class2) at the first line of the class3.method1 override.
Then class2.method1 will be called first and subsequently the Class3.Method1 can add additional code.
Using
inherited is highly dependant on the use case and the code in question and is in no way required by the language itself.
Yes it is possible, there should be no error or warning.
But not his way: the warning appears when you do not call inherited!! See my example, that is warning free.
If you comment out the inherited calls the warning appears.
No, the compiler issues the warning once it has parsed the
declaration. It couldn't care less about the
implementation at that point and thus doesn't know anything about you using
inherited. The warning appears because a new method chain is introduced with the
class2.method1 being declared as
virtual and the solution is either to declare it as
override (to continue the method chain started by
class1.method1) or to declare it as
reintroduce as well to suppress the warning (and thus starting a new method chain).