Suppose I want to override some class which is already defined somewhere, for example, let’s say System.Classes.TOriginalClass, and I want to keep the original class name. I know I will have to define it something like this:
type
TOriginalClass = class(System.Classes.TOriginalClass)
...
end;
If I wanted to implement this custom overridden class within the same unit where it was declared, I don’t have any problem, so long as every place where I want it used is Below where I declared this class.
But suppose I want to use this replacement class in another unit. System.Classes is an extremely common unit, which is used in almost every other unit of mine. I would like to be able to use both the System.Classes unit as well as the unit where I have created this overridden version, let’s call it MyClasses.
I know that in any other unit, I could accomplish this by including the full unit/class in the declaration, like so:
type
TMyClass = class(MyClasses.TOriginalClass)
...
end;
But, that would pretty much defeat the whole purpose, because I would be forced to include MyClasses. as a prefix before the TOriginalClass. What I would like to do is not have to worry about where TOriginalClass was originally declared, and declare it like this…
type
TMyClass = class(TOriginalClass)
...
end;
…while being sure that TMyClass will be inherited from MyClasses.TOriginalClass instead of System.Classes.TOriginalClass.
I’m pretty sure this will consist of the order in which I declare each of these two units in the uses clause, for example, using System.Classes before using MyClasses. But is this supposed to be possible? What should I be aware of when doing this?
PS – TOriginalClass I know isn’t a real class, I’m using it to demonstrate any class which was originally declared in System.Classes. The same should apply for any original class in any unit.
You already gave the answers.
Yes, you can derive from
MyClasses.TOriginalClassby only specifyingTOriginalClass, but it requires thatMyClassescomes later in theuseslist thanSystem.Classes, or thatSystem.Classesis omitted from theuseslist altogether. There is no other way, sorry.If you don’t qualify
TOriginalClass, the compiler will use the “last” declaration it encountered. It will not automatically use the most derived one.Sorry.