简体   繁体   中英

Cast an object to another class

I have three classes.

Where currentsplit is declared as static and accesed in mid.java

DecDriver.java

public class DecDriver extends Configured implements Tool {
    public static Split currentsplit=new Split();
    public static void main(String[] args) throws Exception {
    }
}

Split.java

import java.util.ArrayList;
import java.util.List;

public class Split{
    public  List attr_index;
    public  List attr_value;
    double entophy;
    String classLabel;
    Split()
    {
        this.attr_index= new ArrayList<Integer>();
        this.attr_value = new ArrayList<String>();
    }
    Split(List attr_index,List attr_value)
    {
        this.attr_index=attr_index;
        this.attr_value=attr_value;
    }
    void add(Split obj)
    {
        this.add(obj);  
    }
}

Mid.java

DecDriver id = new DecDriver();
for(int count=0;count<size_split;count++)
{
    index=(Integer) id.currentsplit.attr_index.get(count);
    System.out.println("index : "+index);
    attr_value=(String)id.currentsplit.attr_value.get(count);
    System.out.println("attr_value : "+attr_value);     
}

But I have a scenario where I should write currentsplit object to a file from DecDriver and access that file in Mid.java and proceed further.

How can I do that? What I did is converted the currentsplit object to string and wrote in file using bufferedwriter.

DecDriver.java

public class DecDriver extends Configured implements Tool {
    public static Split currentsplit=new Split();
    public static void main(String[] args) throws Exception {
    String objtostring = currentsplit.toString();
    //Buffered writer
    sptbw.write(objtostring);
    .
    .
}

Then I tried to read the file in Mid and casted to Split object.

Object s = null ;
String cursplitinfo;
//BufferedReader 
while ((cursplitinfo = splitpathbw.readLine()) != null)   {
    s = cursplitinfo;
}

Split currentsplitobj = (Split) s;

DecDriver id = new DecDriver();
for(int count=0;count<size_split;count++)
{
    index=(Integer) currentsplitobj.attr_index.get(count);
    System.out.println("index : "+index);
    attr_value=(String)currentsplitobj.attr_value.get(count);
    System.out.println("attr_value : "+attr_value);
}

But when I tried to run my program it is showing:

java.lang.Exception: java.lang.ClassCastException: java.lang.String cannot be cast to pck.Split

Am I doing anything wrong?

Why don't you used JAVA Serialization to write object into file.
You can take reference of how to write object into file Here .

Example :
Write object into File.

Split currentsplit=new Split();  

FileOutputStream fout=new FileOutputStream("f.txt");  
ObjectOutputStream out=new ObjectOutputStream(fout);  

out.writeObject(currentsplit);  
out.flush();  

Read object from file.

ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));  
Split  s=(Split)in.readObject();  

The error is pretty clear.

You cannot cast a Split object to String Object.

When you do

s = cursplitinfo;

s still holding a type String.

and

Split currentsplitobj = (Split) s;

How can a String behaves as Split object now ?? No.

All Human beings are Animals but not every animal is a Human being.

Base b = new Derived(); //reference variable of Base class points object of Derived class
Derived d = b; //compile time error, requires casting
Derived d = (Derived) b; // type casting Base to Derived

OR : Here is a simple Example.

//X is a supper class of Y and Z which are sibblings.
public class RunTimeCastDemo {

    public static void main(String args[]) {
        X x = new X();
        Y y = new Y();
        Z z = new Z();
        X xy = new Y(); // compiles ok (up the hierarchy)
        X xz = new Z(); // compiles ok (up the hierarchy)
        //      Y yz = new Z();   incompatible type (siblings)
        //      Y y1 = new X();   X is not a Y
        //      Z z1 = new X();   X is not a Z
        X x1 = y; // compiles ok (y is subclass of X)
        X x2 = z; // compiles ok (z is subclass of X)
        Y y1 = (Y) x; // compiles ok but produces runtime error
        Z z1 = (Z) x; // compiles ok but produces runtime error
        Y y2 = (Y) x1; // compiles and runs ok (x1 is type Y)
        Z z2 = (Z) x2; // compiles and runs ok (x2 is type Z)
        //      Y y3 = (Y) z;     inconvertible types (siblings)
        //      Z z3 = (Z) y;     inconvertible types (siblings)
        Object o = z;
        Object o1 = (Y) o; // compiles ok but produces runtime error
    }
}

Just visit this link to see casting with OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM