Skip to main content

In java, how to cast a class to another class?

1 reply [Last post]
monodummy
Offline
Joined: 2009-12-04
Points: 0

I have 5 classes, all of them have getID() property .

I want to cast them to this class
Class Anygirl{
int id =0;

public int getId(){
return id;
}
}

and later do this
ClsE e = new ClsE();
Anygirl x = (Anygirl) e;
int a = x.getId();
System.out.println(a);

Is this possible?
What is the right way to do this?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chandru2010
Offline
Joined: 2010-01-01
Points: 0

do the class by this way:

class A {
nt id =0;

public int getId(){
return id;}
}
class B extend A{

P.S.V.main(strg ){
B b=new A();
}
}