Is there a way to prevent method failure with downcasting in a superclass?

I am trying to understand whether the answer to the following question is the same in all the main OOP languages; and if not, how different are these languages.

Suppose I have a class A that defines act and jump methods; The act method calls the jump method. A subclass B overrides the jump method (i.e., the appropriate syntax is used to ensure that the call to implementation is implemented in class B ).

I have an object B class B I want him to behave as if he were class A In other words, I want jump executed using an implementation in A What are my options in different languages?

For example, can I achieve this with some form of contraction? Or perhaps by creating a proxy object that knows which methods to call?

I would like to avoid creating a completely new class A object and carefully set up sharing of the internal state between A and B , because it is clearly not promising and difficult. I would also like to avoid copying state B to a new class A object, because there can be a lot of data to copy.

UPDATE

I asked this question specifically about Python , but it seems that this cannot be achieved in Python , and technically it can be done ... kinda ..

In addition to technical feasibility, there seems to be a strong argument against this in terms of design. I ask about this in a separate question .

0
source share
4 answers

Repeated Comments: Prefer composition over inheritance.

Inheritance works well when your subclasses have clearly defined behavioral differences from their superclass, but you often get to the point where this model becomes uncomfortable or no longer makes sense. At this point, you need to rethink your design.

Composition is usually the best solution. Delegating the behavior of your object to another object (or objects) can reduce or eliminate your need for a subclass.

In your case, the behavioral differences between classes A and class B can be encapsulated in a strategy template. Then you could change the behavior of class A (and class B, if it is still required) at the instance level by simply assigning a new strategy.

A strategy template may require more code in the short term, but it is clean and maintained. The swizzling method, the monkey patch, and all those cool things that allow us to poke in our particular language implementation are funny, but the probability of unexpected side effects is high, and the code is usually difficult to maintain.

+2
source

What you are asking is completely unrelated / not supported by OOP software.

If you subclass an object A with class B and redefine it to the method when a specific instance of B created then all overlap / new implementation of the basic methods associated with it (either we are talking about Java or C ++ with virtual tables, etc.).

You have an instance of object B
Why do you expect that you /////////////////////////////// should /

You could call it explicitly, for example, for example. calling super inside the method, but you cannot do it automatically, and casting will not help you either.

I can’t imagine why you would like to do this.
If you need to use class A , then use class A
If you need to override its functionality, use its subclass B

+1
source

Most programming languages ​​face some problems in supporting dynamic dispatch of virtual functions (the case of invoking an overridden jump method in a subclass instead of implementing the parent class) - to the extent that it works or avoids it is difficult. In general, specialization / polymorphism is a desirable feature - perhaps the goal of OOP in the first place.

Take a look at the Wikipedia article on Virtual Functions , which provides a useful overview of virtual function support in many programming languages. This will give you the opportunity to start learning a specific language, as well as the trade-offs that you can weigh when viewing a language in which a programmer can control the dispatch behavior (see, for example, the section on C ++).

So freely, the answer to your question: "No, the behavior is not the same in all programming languages." In addition, there is no language-independent solution. C ++ may be the best choice if you need behavior.

0
source

You can do this using Python (sort of), with some terrible hacks. This requires that you implement something like the wrappers that we discussed in your first question posed in Python, but as a subclass of B. Then you need to implement write proxies as well (the wrapper object should not contain any state, usually related with a class hierarchy, it should redirect access of all attributes to the base instance of B.

Instead of redirecting the method search to A and then calling the method with a wrapped instance, you call the method that passes the wrapper object as self . This is legal because the wrapper class is a subclass of B, so the wrapper instance is an instance of the classes whose methods you call.

This would be very strange code, requiring dynamic generation of classes while using the IS-A and HAS-A relationships. It will probably also turn out to be rather fragile and bring strange results in many angular cases (as a rule, you cannot write 100% perfect wrapper classes in Python precisely because this strange phenomenon is possible).

I completely get away from the weather, is it a good idea or not.

0
source

Source: https://habr.com/ru/post/1390703/


All Articles