Getter / Setter from separate class file in Java

I'm new to Java and to assign HW we needed to create a Person class that has a constructor, getter / setter for the firstName, lastName, phone attributes. This is in a separate file from the old HW assignment (Person.java). We now have to use this Person class in our new HW assignment (LoanApplication.java). Therefore, if one of the attributes

private Person client

      

Do I need to create getter / setters or constructor again? Otherwise, how does each LoanApplicaiton instance know which Person attribute it should go to?

How does the JVM know it can use Person.class even though my LoanApplicaiton.class doesn't extend Person.class? Thanks.

+2


a source to share


2 answers


No, if you spell it correctly class Person

in Person.java

, you can just use it in class LoanApplication

in LoanApplication.java

. You Person.java

don't need to change at this point , but you should make sure the Java compiler can find it when compiling LoanApplication.java

.

This means that you may need to add multiple statements import

or, more simply, perhaps you just need to copy Person.java

to the same home directory as LoanApplication.java

.

You don't need to extends

use an existing class. It is best to write instead of inheritance (see Effective Java 2nd Edition: Clause 16: Positive Composition over Inheritance). For example, Person.java

probably uses String firstName

etc. This means it is class Person

composed using String

(among other things), but it does not inherit from String

(which is equal final class

anyway).




How does the JVM know what it can use Person.class

even though mine is LoanApplication.class

n't extending Person.class

?

It has nothing to do with inheritance. Even if class LoanApplication extends Person

(this is a HORRIBLE problem, by the way), the JVM will still have to find Person.class

to download LoanApplication.class

(how the JVM finds .class

files is another problem!)

To be honest, I don't think it is useful to worry about such details right now. If you are serious about programming, I recommend getting an IDE like Eclipse ; it will take care of file management, linking, compilation, etc. so that you can focus on the real task, which is programming.

0


a source


No, you don't need to redefine Personality. Just add the compiled "* .class" file to your classpath and add the appropriate import statement to use instances of the Person class. This is, in fact, one of the benefits of object-oriented programming; old objects can be reused in new projects (provided they are well designed and "do one thing and something good" and are not tied to specific application logic).

I don't know what you mean by "how each LoanApplicaiton instance knows which Person it should go to", but assuming your getters / setters are public, you can call these functions from your LoanApplication class. For example, if a Person has a function called "setName ()", you have an instance of the person in your loan application named "p" and name "n", then you can call "p.setName (n)" to set p name to n.



The Java Virtual Machine (JVM) has this concept, called the "classpath", which determines where it looks for the "* .jar" and "* .class" files that contain the definitions of various classes. Classes are saved in a format that the JVM can read and accept its definitions. As for non-proliferation ... there are several different ways objects interact in an object-oriented programming language; inheritance (one object extends another), polymorphism (several different classes provide different implementations for the same interface ... this is a specific inheritance application), composition (a class consists of instances of other classes), delegation (one delegate class its work in one or multiple instances of other classes, which is a special case of composition),among many other OOP design patterns. In this case, you need composition.

+1


a source







All Articles