How to tell whom a class will extend at runtime?

Asked

Viewed 77 times

2

I have the class

public class Conhecimento{}

This class extends from another class "Cteproc"

Only this Cteproc class has "versions" example.

V200.Cteproc

V300.Cteproc

My problem is that in creating the Knowledge class I need to tell if it will extend from Cteproc 200 or Cteproc 300 according to the version I receive. I was wondering if it is possible to do this, change the extends or leave Dynamic.

  • Create class at runtime?

  • No, the classes already exist, I just want to inform of whom a class will extend at runtime.

  • There is no way, if the class is already written, as you will change after compiled, it is apparently impossible. I thought you wanted to write the class at runtime.

  • I did not understand. Or if I understood, it is normal. If understanding is the opposite of what I am thinking, then it does not and would not make sense. If you clarify, maybe put an example, I try to comment better or answer. Is that what Articuno is saying? Maybe you can create a project pattern.

  • I will edit the question.

  • If you want to do this you’d better use it alone Generics, it doesn’t make much sense to use inheritance like that.

  • 3

    It would be better if you explained what problem you want to solve.

  • Yeah, it sounds like a scam, and there’s got to be another way to solve it.

  • @bigown if I didn’t get it wrong, it seems to me that he wants to alter the inheritance at "run time". Wouldn’t that be the same as "rewriting the class"? It seems to me a bit impossible, since in the execution, the class is already compiled.

  • 2

    @Articuno seems architectural error, if there are two classes to be inherited, there must be two inherited classes, simple like this, but if the problem is another there may be a solution, it may be a case of composition, maybe using a Pattern specific.

  • This 'according to the version you receive'. You receive how? What defines that one or the other will be? And what is the difference between one and the other? The methods of both classes are the same but with different executions? It seems to me that you want to use inheritance and polymorphism. But it’s missing an understanding of how it works there can’t fit your problem.

Show 6 more comments

1 answer

3


It is not possible for a class at one time to inherit from A and at another time to inherit from B.

If I understand what you want, use one of these approaches:

  • Bridge Design Pattern

    If the classes V200.Cteproc and V300.Cteproc implement the same interface (Cteproc), that is the two classes have the same methods but with different implementations.

    Implement the Knowledge class to receive an object from a class that implements the Cteproc interface.

    According to "the version I get" you pass a V200.Cteproc or V300.Cteproc object.

    Internally, the Knowledge class uses the interface methods whose implementation is given by the past object.

  • Adapter Design Pattern

    If the V200.Cteproc and V300.Cteproc classes do not implement the same interface, that is, the two classes have different methods (names or signatures).

    Extract an interface from the Knowledge class.

    Write two classes that implement this interface: V200.Knowledge and V300.Knowledge.

    For processing, the V200 class.Knowledge will internally use a V200.Cteproc object and the V300.Knowledge a V300.Cteproc.

    According to "the version I get" you use a V200 object.Knowledge or V300.Knowledge.

    Clients of the old Knowledge class should reference the interface.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.