Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cast Object instance to Its Original Type Class again 1

Status
Not open for further replies.

soldieryap

Programmer
Aug 15, 2000
59
0
0
MY
sorry that i mess up the question and thanks for all the guys in my previous question > Object Casting Question < i would like to clearly define my question again.

Away from polymorphism or something else, i would like to know is there any solution/function that can cast an Object type instance to its original Type(eg, Dog)?

eg. Dog dog = new Dog();
Object obj = dog();
((XX) obj).sound(); // how i cast it back?
// and i don't expect to use
// ((Dog) obj).sound();

when i wanna find out the original type of the obj, i use obj.getClass().getName(); but what i want is not find out the Class name it actually is instead of casting that Object type (obj) to Dog. just because of some other reason i have to cast it to Object and i have to cast it back now and perform some function only exist on that Class. and i don't want to use ((Dog) obj).sound(); because i may write lots code of &quot;instanceof&quot; just to test out which Class is actually belongs to.

do u guys got what i mean??
thanks for ur notice and sorry for my poor explaination!
if u still don't understand please state it out.


 
BTW: if ur classes are all unrelated and somehow u're dumping it all into a vector or something then u're definitly approaching this problem incorrectly.
 
Refined code (the original code meant the object you invoked was not the same as the real object):
Code:
import java.util.*;
import java.lang.reflect.*;

class Dog {
  String name;
  Dog(String name) { this.name = name; }
  public void sound() { System.out.println( name + &quot; barks&quot; ); }
}
class Cat {
  String name;
  Cat(String name) { this.name = name; }
  public void sound() { System.out.println( name + &quot; meows&quot; ); }
}

public class Casting {

  private static void stroke( Object o ) {
    Class c = o.getClass();
    try {
      Method m = c.getMethod( &quot;sound&quot;, null );
      m.invoke( o, null );
    } catch( Exception e ) {}
  }

  public static void main( String[] args ) {

    Object[] pet = {
      new Dog( &quot;Spot&quot; ),
      new Cat( &quot;Mr. Tiddles&quot; )
    };

    for( int i = 0; i < pet.length; i++ )
      stroke( pet[i] );
  }
}
Ugh [thumbsup2]
 
Just out of curiosity, what impact (if any) does this have on the security of programs? Can you access instances of important vm classes and try to call different methods? I can't imagine that trying to call obliterate() on an unknown class is a good thing.
 
Good point havathejut.. i thought u gave up on this post already heh.. If that code works then it seems like this whole &quot;ENCAPSULATION&quot; idea failed miserably.
[afro2]

T
 
actually i just tried to make my class more portable!

my classes are actually not a big deal classes, i have several classes inherits JFrame, because of the reason self define methods, i can't casts them to Jframe and perform the functions.

but afterwards i found it can't really good! and i change the ways to continues with what i wanna do :)

although i don't use that techniques anymore, at least i learned it [sunshine]

thanks guys !!! [pipe]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top