Yes, an interface is inherited by a subclass.
This is perfectly acceptable for translation from a subclass to an interface.
However, and apologies if I read your question incorrectly, but if "and then back to its original class" means.,.
You have interface I, class A, and class B. A implements I, and B inherits A, you probably can, but REALLY DO NOT throw from A to B.
EDIT:
You want to go from B to Z and back to B., but you already have a link to B if B is what you pass to your function, so you don’t need to drop from I to B (unless you talked about something else object, then No, do not do this)
The transition from I to B is the same as the transition from A to B, you are trying to transfer the chain of inheritance, which you really should not do. The need for this is the smell of code, it tells you that you should try to solve this problem differently (perhaps redesigning your classes (for example, adding more properties / methods to I) or just deciding that the function will work only with the subclass - work with subclass “B” will give you access to all methods A and I).
Can you change your question and add sample code for what you are trying to do?
EDIT 2
procedure SendInterfaceObject(iobj:IMyInterFace); begin if (iobj is TSubMyObject) then begin //code here end; end;
The statement “If” has a bad idea and breaks the principles of OO. If you need to do this, then
- Defining an interface is not enough, you can add Enter a property into the interface allowing you (if iObj.SomeProperty = 1), then.,.)
- The interface is simply not the right solution for this problem and you must pass the link as TSubMyObject.
EDIT 3:
@mghie: I agree with you that I did not explain very well that SomeProperty has some data that allows the function to be debugged there, removing the type-checking dependency. SomeProperty should not simply replace type checking (for example, by putting the class name in a property and then checking the class name). This is really the exact same problem.
There is a significant difference between subclasses that inherit from an interface. This difference must be expressed either
- Providing some data item that can then be used on the shoulder
eg.
if(item.Color = Red) then item.ContrastColor := Blue; else item.ContrastColor := Red;
- Or through polymorphism, for example
IEmployee defines the CalculatePay method, TManager and TWorker implement IEmployee, each of which has a different logic in the CalculatePay methods.
If the goal was to do something like the first case, polymorphism may be excessive (polymorphism does not solve every problem).
EDIT 4
You say that the sections here // have nothing to do with the object that was passed to him ... I'm sorry, but this expression is incorrect, if you need to pay an employee, you need to know them 1) EmployeeCode 2) Their salary data 3) Their bank details, etc. If you charge the account that you need 1) InvoiceNumber 2) The invoice amount 3) CustomerCode for payment, etc., this is an ideal place for polymorphism .
Suppose that the function that performs the interface check checks whether the Accounts need to do something with the object (for example, pay an employee, pay an invoice, etc.). So we could call the AccountsCheck function. Inside the account verification, you will have your own logic specific to each subclass (pay for an employee, charge an invoice). This is an ideal candidate for polymorphism.
On your interface (or on another interface or as a virtual method on a subclass) Define the "AccountsCheck" method. Then each derived class gets its own account verification implementation.
The code moves from your simple AccountsCheck function to smaller functions for each subclass. It does the code
- More obvious in intent (each class contains some logic for AccountsCheck)
- You are less likely to break SubClass B when committing anything in AccountsCheck for C
- It’s easier to understand what exactly is the logic for checking the SubClass B account, you should check only 20 lines of code in Small AccountsCheck, and not 200 in the General account)
There are more “good reasons” for this, aif nyone wants to edit / post comments, please do so.
If you find that you need common logic between AccountsCheck implementations, create some utility functions, do not redefine the same wheel in each of them.
Polymorphism is the solution to your problem.