Domanda

I know downcasting is not doable. But I am trying to work around it.

This is what I have.

public class Ticket{
   public int number;
   public String description;
}

public class MyTicket extends Ticket{
   public String status;
}

But in my app, I want to use the MyTicket class because I don't want to force the original Ticket object to change. So when the Ticket object comes back from a call (webservice, DB, etc), I try to downcast to a MyTicket and it fails obviously.

MyTicket mt=(MyTicket)ws.getTicket(1234);

So I was trying to figure a way around this. I was thinking of writing a "copyAttributes" method or copy the attributes within the constructor of the MyTicket class, something like this:

MyTicket mt=new MyTicket(ws.getTicket(1234));

public class MyTicket extends Ticket {
    public String status;
    public MyTicket(Ticket tckt){
        //copy tckt attributes to MyTicket attributes
    }
}

Is there a way to get the attributes of a class and set them into another class? Or is there a totally different way to downcast and I'm missing it?

*SOLUTION:*So I took the solution below and came up with this. I needed the change to return null if the main ticket is not found before the transfer happens:

public class MyTicket extends Ticket {
    public String status;
    public MyTicket(){}
    public static MyTicket newInstance(Ticket tckt){
        MyTicket mytkt=null;
        if(tckt!=null){//copy tckt attributes to MyTicket attributes
            BeanUtilsBean.getInstance().getConvertUtils().register(false,true,-1);
            mytkt = new MyTicket();
            BeanUtils.copyProperties(mytkt, tckt);
        }
        return mytkt;
    }
}
È stato utile?

Soluzione

I think you are doing right. If your object grows, you may want to use Apache BeanUtils to assist you in attrbute copying.

Altri suggerimenti

Downcasting should be used only when you know that the reference is actually of subclass type. It seems the Ticket object returned by the web service is not actually a MyTicket and hence the downcasting would throw ClassCastException at runtime.

Coming to using Ticket returned by the web service while constructing MyTicket, I would define a copy constructor in the Ticket class that would take a ticket object and copies the attributes. This copy constructor will be called in the MyTicket's constructor that takes a Ticket object.

public class Ticket{
   public int number;
   public String description;

   public Ticket(Ticket ticket)
   {
       this.number = ticket.number;
       this.description = ticket.description;
   }
}

public class MyTicket extends Ticket{
   public String status;

   public MyTicket(Ticket ticket)
   {
       super(ticket);
   }

   public MyTicket(Ticket ticket, String status)
   {
       super(ticket);
       this.status = status;
   }
}

Why not just use aggregation + getters/setters in this case?

public class MyTicket {
  private Ticket ticket;
  private String status;

  public MyTicket(Ticket ticket) {
    this.ticket = ticket;
  }

  public int getNumber() { return ticket.number; }
  public void setNumber(int number) { ticket.number = number; }
  public String getDescription { return ticket.description; }
  public void setDescription { ticket.description = description; }
  public String getStatus() { return status; }
  public void setStatus(String status) { this.status = status; }
}

You can then create you object just as you suggested:

MyTicket mt = new MyTicket(ws.getTicket(1234));

I'd suggest you to implement something like a Cloneable (but not the standard Java one, here it might be okay to provide an abstract method in your super class) interface. Something like this in the MyTicket class:

@Override
public Ticket clone() {
    // copying stuff by calling a "copy constructor"
    return new MyTicket(this);
}

private MyTicket(MyTicket t) {
}

If the object you are getting back was created as a Ticket object not a MyTicket then I would approach it the way you have proposed with the copy constructor.

The nicest way, with the least redundant code, would be for your MyTicket to contain the original Ticket instance (aggregation) and use its attributes where needed, without copying anything in advance.

public class MyTicket extends Ticket {
  private final Ticket t;
  public MyTicket(Ticket t) {
    this.t = t;
  }
}

If you need to expose all properties through getters, then this won't be of much use, though. It would only help if you needed Tickets properties internally for some calculations.

Inheritance:

public class ThisIsASubClass extends ThisIsASuperClass{
{
    //declare stuff here
    Object o = "Let's presume a string is passed";
    super(o); //runs the super class (ThisIsASuperClass in this case), sending an object if needed (effectivly running the super class inside of the subclass)
    //put the subclass code here
 }

A good example I found (it's long, but I have no problem reading long things. If you are a programmer, you shouldn't either.):

Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects lesson:

public class Bicycle {

// the Bicycle class has
// three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has
// one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

// the Bicycle class has
// four methods
public void setCadence(int newValue) {
    cadence = newValue;
}

public void setGear(int newValue) {
    gear = newValue;
}

public void applyBrake(int decrement) {
    speed -= decrement;
}

public void speedUp(int increment) {
    speed += increment;
}

}

A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:

public class MountainBike extends Bicycle {

    // the MountainBike subclass adds
    // one field
    public int seatHeight;

    // the MountainBike subclass has one
    // constructor
    public MountainBike(int startHeight,
                    int startCadence,
                    int startSpeed,
                    int startGear) {
    super(startCadence, startSpeed, startGear);
    seatHeight = startHeight;
}   

// the MountainBike subclass adds
// one method
public void setHeight(int newValue) {
    seatHeight = newValue;
}   
}

MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug. What You Can Do in a Subclass

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

The following sections in this lesson will expand on these topics. Private Members in a Superclass

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass. Casting Objects

We have seen that an object is of the data type of the class from which it was instantiated. For example, if we write

public MountainBike myBike = new MountainBike();

then myBike is of type MountainBike.

MountainBike is descended from Bicycle and Object. Therefore, a MountainBike is a Bicycle and is also an Object, and it can be used wherever Bicycle or Object objects are called for.

The reverse is not necessarily true: a Bicycle may be a MountainBike, but it isn't necessarily. Similarly, an Object may be a Bicycle or a MountainBike, but it isn't necessarily.

Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write

Object obj = new MountainBike();

then obj is both an Object and a Mountainbike (until such time as obj is assigned another object that is not a Mountainbike). This is called implicit casting.

If, on the other hand, we write

MountainBike myBike = obj;

we would get a compile-time error because obj is not known to the compiler to be a MountainBike. However, we can tell the compiler that we promise to assign a MountainBike to obj by explicit casting:

MountainBike myBike = (MountainBike)obj;

This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume that obj is a MountainBike. If obj is not a Mountainbike at runtime, an exception will be thrown. Note: You can make a logical test as to the type of a particular object using the instanceof operator. This can save you from a runtime error owing to an improper cast. For example:

if (obj instanceof MountainBike) {
    MountainBike myBike = (MountainBike)obj;
}

Here the instanceof operator verifies that obj refers to a MountainBike so that we can make the cast with knowledge that there will be no runtime exception thrown.

TL;DR: if you use inheritance, any square is a rectangle, but not all rectangles are squares..

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top