java - Single 'load from datastore' method for supertype usable when used as subtype -


i have 2 classes, person , employee. employee extends person.

i have method reads person storage, , writing method reads employee.

i'd reuse method have person read properties same employee, without copy-pasting code, can't seem find way it.

public person getpersonfromstorage() {     person person = new person();     // ... logic     return person; }  public employee getemployeefromstorage() {     employee employee = new employee();     // ... logic employee-specific fields      // want read fields inherited person here     return employee; } 

i cannot cast retrieved person getpersonfromstorage, because not employee. be, because it's not subtype either, it's not.

i following:

public person getpersonfromstorage(person person) {     if(person==null) { person = new person(); }     // ... logic     return person; }  public employee getemployeefromstorage() {     employee employee = (employee) getpersonfromstorage(new employee());     // ... logic employee-specific fields     return employee; } 

but i'd avoid complexity if can. have feeling i'm overlooking elementary. there better way solve this?

just offering different architecture use in instance. if you're talking "from storage", me means sort of persistent structure. text file, database, etc. following example, let's assume have values in text file.

assume file employees.txt, contains 1 employee:

// dave's person details. dave manchester 32 // dave's employee details assassin north korea. 

then you've got class person, looks little this:

public class person {     private string name;     private string location;     private int age;      public person(string name, string location, int age)     {        // blah blah blah.     } } 

and class employee looks this:

public class employee extends person {     private string jobtitle;     private string area;      public employee() {         // etc.     } } 

in person class, can create constructor designed read parameters person. like:

public person(scanner file) {     this.name = file.nextline();     this.location = file.nextline();     this.age = file.nextint();     file.nextline(); // make sure you're pointing @ new line! } 

and in employee class, can create constructor designed read parameters employee, while calling it's superclass deal other values.

public employee(scanner file) {     super(file);     this.jobtitle = scanner.nextline();     this.area = scanner.nextline(); } 

then have call like:

scanner s = new scanner("employees.txt"); person p = new employee(s); 

or make more compact:

person p = new employee(new scanner("employees.txt")); 

this go , parse file, , return object, while wrapping logic reading file inside classes need data.

not text file?

well, it's not vital. important thing passing object call chain, , methods doing that particular class needs do, passing on object. if it's database row, it's same principle.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -