Skip to main content

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

1 reply [Last post]
Joined: 2009-12-04

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();

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.
Joined: 2010-01-01

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();