oop - Java - Converting a generic class' instance to specialized class' instance -


i struggling fundamental concept of oop in java.

class person {}  class parent extends person {} 

all parents people, not people parents.

person adam  = new person(); adam  = new parent(); // means variable 'adam' referencing new object. adam = (parent)adam; // java.lang.classcastexception 

how express idea of becoming subclass in java? 1 have write constructor subclass takes superclass object example...

public class person {      private string name;      public person(string name){         this.name = name;     }      public string getname() {         return this.name;     }  } 

and parent class like:

public class parent extends person {     public parent(person p) {         super(p.getname());     } } 

output:

public class run {      public static <t> void display(t x) {         system.out.println(x);     }      public static void main(string[] args) {          person adam  = new person("adam");          display(adam instanceof person); // true         display(adam instanceof parent); // false          // lets assume adam has had child.          // adam has 'become' parent. still person.          adam = new parent(adam);          display(adam instanceof person); // true         display(adam instanceof parent); // true     } } 

is way? there better way? how 1 express idea of becoming in java. say, starting off reference general object, , becoming more specialized.

** edit clarification:

let assume have class t

let assume have variable v of type t (v :: t)

we set v reference new object of type t.

t v = new t(); 

(v :: t) -> (obj-t :: t)

we have class s subclass of type t. may or may not have additional methods.

i create new object obj-s of type s copies of information (instance variables) obj-t.

allowing me use existing variable (v :: t) refer new object.

v = new s(v); 

(v :: t) -> (obj-s :: s)

thus obj-s instance of both s , t, , use of v valid s subclass of t.

my question if method correct?

i consider thing you're trying sort of type juggling php uses; 'reference type' can changed @ runtime.
java, in contrast, uses typed variables. define reference call something, is, variable name, in our case type person. can speak 'link' object variable name. object can of type, long of type person.

with code adam = new parent(), assign variable adam (which of type person, declared it) new parent instance. note object referred adam (the 1 created new person()), still in memory, has no reference it. once garbage collector comes by, object erased memory.
once declare reference adam type person, cannot redefine variable adam have type parent. reference adam has type person, once declare way.

if know sure object reference adam pointing of type parent, can cast safely:

((parent) adam).dosomethingonlyparentswoulddo(); ((parent) adam).raisechild(); 

alternatively, can assign object of reference adam new variable of subtype, this:

parent padam = (parent) adam; padam.dosomethingonlyparentswoulddo(); padam.raisechild(); 

if want copy information or person parent class, must create copy constructor:

public parent(person person) {     // assuming fields protected     this.prop = person.prop;     this.anotherprop = person.prop; } 

or static creator method:

public static parent createfromperson(person person) {     parent p = new parent();     // assuming fields protected     this.prop = person.prop;     this.anotherprop = person.prop;     return p; } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -