tags:

views:

61

answers:

2

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.

A: 

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.

polygenelubricants
+1  A: 

No, you do not need to reimplement Person. Simply add the compiled "*.class" file to your classpath, and add the appropriate import statement in order to use instances of the Person class. This is, in fact, one of the benefits of object-oriented programming; old objects may be reused in new projects (assuming that they are well-designed and "do one thing and one thing well" and aren't coupled with application-specific logic).

I do not know what you mean by "how does each LoanApplicaiton instance know which Person attribute it is to go with", but assuming your getters/setters are public, then you can invoke those functions from your LoanApplication class. For example, if Person has a function called "setName()", you have a person instance in your loan application named "p", and a name "n", then you can call "p.setName(n)" to set p's name to n.

The Java Virtual Machine (JVM) has this concept called the "classpath" which determines where it looks for "*.jar" and "*.class" files, containing the definitions of various classes. The classes are saved in a format that the JVM is capable of reading and for taking their definitions. As for not extending it... there are several different ways that objects can interact in an object-oriented programming language; inheritance (one object extends another), polymorphism (multiple different classes provide different implementations for the same interface... this is a particular application of inheritance), composition (a class is composed of instances of other classes), delegation (one class delegates its work to one or more instances of other classes, which is a special case of composition), among many other OOP design patterns. In this case, you need composition.

Michael Aaron Safyan