Reputation: 313
I have read that Inheritance is a "compile-time' phenomenon. Also in a different place I have read that the superclass code is loaded by classloader, which I deduce happens at run-time. This is causing me some confusion regarding the nature of inheritance. Does the class file of sublass contain the actual compiled code of superclass, or is it accessed at run-time?
Upvotes: 5
Views: 1845
Reputation: 10394
Where did you read it's compile time? I guess if you're compiling your subclass then yes, it needs to have a superclass to reference when being compiled.
But when you actually run the code it is dynamically linked as per:
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
"The Java Virtual Machine dynamically loads, links and initializes classes and interfaces"
Upvotes: 1
Reputation: 6667
So consider you create a class that inherits a class that is included in a 3rd party jar file.
In order to compile your code you need to have the 3rd party jar file in the classpath of your compiler.
In order to run your code you will also need the jar file in the classpath of the java command that launches the application.
Your subclass does not contain the code of the superclass, it is in the jar files. Your compiled class contains a reference to the superclass. When your class is loaded by the classloader it searches the classpath for the superclass and loads it.
Upvotes: 4