You'll need the class
file in order to actually use the class, but that doesn't mean you'll need the source file.
Let's take a look at the possibilities using the Dog
and Animal
class example. Assuming that the Dog
class is a subclass of the Animal
class, we can do the following:
If you have both Animal.class
and Animal.java
, you can make a Dog
class.
If you have Animal.class
but not Animal.java
, you can make a Dog
class.
If you don't have Animal.class
but have Animal.java
, you can make a Dog
class. (This means that the Animal.java
file will need to be compiled when the Dog.java
file is compiled.)
If you don't have either Animal.class
nor Animal.java
, you cannot make a Dog
class.
Here is a tabular version of the above:
Have Animal.java? Have Animal.class? Can make Dog class?
----------------- ------------------ -------------------
Yes Yes --> Yes
Yes No --> Yes
No Yes --> Yes
No No --> No
If you have the class
file, there are programs which can decompile the class
file to produce a human-readable java
source file, however, it will not restore the exact source that was used to produce the class
file.
However, in this case, if all that is necessary is to extend the Animal
class to make a new Dog
class, the source code itself is not necessary.
Keep this in mind -- whenever a class is made in Java, it always extends the Object
class. Even if we don't have access to the source code of the Object
class, since we have access to the Object.class
in Java, we are able to create our own classes.
This case is similar -- as long as we have the class
file for a class, we can use it to the fullest extent. The only thing that is missing is the actual implementation which is listed in the source code.