0

I am wondering about replacing Java's 'extends' keyword somehow for dynamically extending a class based on a parameter(file, environment variable, db...basically anything). Is this even possible because playing with class loaders or calling constructors does not achieve this. I am not asking "should I use interface or superclass hierarchy" rather what is extending really mean under the hood in JAVA because there aren't any good description about it just the good old inheritance jargon:

https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

8
  • And what would you do in the class on the basis of this parameter? Why can't you just have multiple instances of a class for the different parameters? (The short answer is: no, you can't. Inheritance is a property baked into a class at compile time) Commented Apr 29, 2016 at 7:53
  • Why do you want to do that? Is this for something like a dynamic proxy? Commented Apr 29, 2016 at 7:55
  • @AndyTurner maybe annotating and reflecting? Commented Apr 29, 2016 at 7:56
  • 3
    This sounds like an XY problem of epic proportions. Please state the problem you are trying to solve, and not some intermediate step to solving it. The short answer here is a resounding no. The longer answer is yes, this is possible, but would require extensive and errorprone load time weaving. Commented Apr 29, 2016 at 7:57
  • 1
    @Zoltan: If you can provide a concrete example of what you're doing and the limitations you're running into (in another question, this question has already sailed), people can provide concrete suggestions for dealing with those limitations. Open-ended "what is extends" probably won't help you with those issues. Commented Apr 29, 2016 at 8:03

3 Answers 3

1

The only way to "replace the extends keyword" is to dynamically create classes at runtime, which is entirely possible but non-trivial. Vert.x is a good example of a project that makes extensive use of dynamically-generated classes.

Java wasn't designed as a dynamic language in that sense. There are several dynamic languages out there (some of which can run on the JVM), such as JavaScript.

rather what is extending really mean under the hood...

Without getting into a long treatise on OOP, when you say Derived extends Base, it means that Derived inherits both the public and protected API of Base (which it can then add to) and also the implementation of that API. It means that code expecting to see a Base instance can accept a Derived instance, because Derived "is a" Base. This link is created a compile-time. At runtime, instantiating an instance of Derived involves all of the plumbing that instantiating a Base instance involves, plus then the added plumbing for Derived.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answers, I will look into this Vert.x project.
0

To achieve this you need to maintain various versions of a class based on the condition and you have to customise class loader as well because at a point when you find that you have to load a particular instance, you need to load that class which is not loaded by default class loader on JVM startup.

Its better to maintain multiple versions of the class and let JVM do its job which it does perfectly.

Comments

0

You can't do that with a language like Java. The information about "inheritance" is not only used by the compiler, it is also "hard-baked" into the compiled byte code.

If you really want to such kind of "dynamic" meta programming; you are better of using languages that allow you to do so; instead of "violating" a language that was never intended for such kind of usage.

To use some stupid comparison: just because you happen to know "screws" and "hammer" ... you wouldn't start to use a hammer to get those screws into the wall, would you? Instead, you would be looking for a tool that works better with "screws" than a hammer.

If you still want your code to run within a JVM; you might consider languages like jython or jruby.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.