No, assuming that you wrote class Person
in Person.java
properly, you can just use it in class LoanApplication
in LoanApplication.java
. At this point, Person.java
does not need to be modified, but you do have to make sure that the Java compiler can find it when you're compiling LoanApplication.java
.
This means that you may have to add some import
statements, or more simply, perhaps you just need to copy Person.java
to the same homework directory as LoanApplication.java
.
You do NOT have to extends
an existing class to use it. A better way often is to compose instead of inherit (see: Effective Java 2nd Edition: Item 16: Favor composition over inheritance). For example, Person.java
probably uses String firstName
, etc. That means that class Person
is composed using String
(among other things), but it doesn't inherit from String
(which is final class
anyway).
How does the JVM know that it can use the Person.class
even though my LoanApplication.class
does not extend Person.class
?
This has nothing to do with inheritance. Even if class LoanApplication extends Person
(which is a HORRIBLE idea, by the way), the JVM would still need to be able to find Person.class
to load LoanApplication.class
(how JVM finds .class
files, that's another issue entirely!)
Honestly, though, I don't think it's helpful to worry about details like this at this point. If you're serious about programming, I recommend getting an IDE such as Eclipse; it'd take care of the file management, linking, compilation, etc for you, so you can concentrate on the real task, which is programming.