I’m new to Java and for a HW assignment, we had to create a Person class that has a constructor, getter/setter for the attributes of firstName, lastName, phone. That is in a separate file from an old HW assignment (Person.java). Now we have to use that Person class in our new HW assignment (LoanApplication.java). So if one of the attributes is
private Person client
do I need to create getter/setters or a constructor again? Otherwise, how does each LoanApplicaiton instance know which Person attribute it is to go with?
How does the JVM know that it can use the Person.class even though my LoanApplicaiton.class does not extend Person.class? Thanks.
No, assuming that you wrote
class PersoninPerson.javaproperly, you can just use it inclass LoanApplicationinLoanApplication.java. At this point,Person.javadoes not need to be modified, but you do have to make sure that the Java compiler can find it when you’re compilingLoanApplication.java.This means that you may have to add some
importstatements, or more simply, perhaps you just need to copyPerson.javato the same homework directory asLoanApplication.java.You do NOT have to
extendsan 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.javaprobably usesString firstName, etc. That means thatclass Personis composed usingString(among other things), but it doesn’t inherit fromString(which isfinal classanyway).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 findPerson.classto loadLoanApplication.class(how JVM finds.classfiles, 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.