1

I am planning on coding an API. I want to call a method from the API super class, that will be declared in the class that extends the API class. Is there a way to do this? Thanks - PrimeCubed

MisterHazza
  • 188
  • 1
  • 10

1 Answers1

1

There is no way. Reason is that a subclass is a different object type. You could try to think about a way to parse your superobject into a subobject.

Edit: I have no idea of what your API is doing or what so ever. If you can, then just post a little more detail. Usually an extension USES the API. So if you think in plugins, then you offer the basic structure and use interfaces to create general objects.

Maybe events are a solution for you?

If you want more concrete hints, then please post more details.

Edit 2: Check out abstract classes and interfaces if you are trying to create an API. That is a very good way to go. And even further, the observer pattern could be useful for registering some listeners etc.

F_Schmidt
  • 902
  • 1
  • 11
  • 32
  • Hi! I am writing an API, that is an interpreter for a language that I designed. I would like the way output is outputted to be defined in a method in the java program that is using the API, so the API can use that method to output strings. – MisterHazza Apr 03 '20 at 10:36
  • Please post some of your code. It is difficult to understand without. – F_Schmidt Apr 03 '20 at 10:38
  • I haven't written any code yet, sorry XD. I am just getting ideas of how my API is going to work. Ideally, I would like a method defined in the API (superclass) that has a body, but another class can change that method, so it runs differently. Is there a way to do that? – MisterHazza Apr 03 '20 at 10:39
  • Yes that is possible. You can create an interface and then your external Javaprogram implements that interface and overwrites the method. Then it behaves differently. Or you write your own language so that it implements the print? also possible. – F_Schmidt Apr 03 '20 at 10:45
  • Can the method be run in the API though? – MisterHazza Apr 03 '20 at 10:46
  • I am sorry, but I can not help you that much, because I have no idea on what you are planning. I wrote a lot of plugins which basically always extended an (abstract) class or interface if the overwrote methods. – F_Schmidt Apr 03 '20 at 10:47
  • Ok thanks for your help. You have given me a lot of ideas of how my program can work ;) – MisterHazza Apr 03 '20 at 10:50
  • No worries. It is quite difficult to get a good structure - but if you have one, you almost automatically get a good program ;) I would recommend collecting ideas, reading into programming patterns and first plan some kind of UML before implementing. Happy programming! – F_Schmidt Apr 03 '20 at 10:51
  • please accept my answer if you are happy with it. If you want more tipps on how to create an API, then please write a new question. – F_Schmidt Apr 03 '20 at 11:01
  • I have accepted it. I have one more question: You said 'You can create an interface and then your external Javaprogram implements that interface and overwrites the method.' Can you give an example of how to do this please? (I am fairly new to java). – MisterHazza Apr 03 '20 at 11:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210864/discussion-between-primecubed-and-f-schmidt). – MisterHazza Apr 03 '20 at 11:08