Reputation: 278
i feel stupid ask this question, but i really dont see the problem. Compiling it gave me ClassCastExeption
This is the super class
package elements;
import persistence.postgres.DataSource;
import persistence.postgres.IdBroker;
import persistence.postgres.PersistenceException;
import util.Util;
public class Hotspot {
private double lat;
private double lng;
private long id;
private String username;
private String time_stamp;
private String point_of_interest;
private String comment;
public Hotspot(double lat, double lng, String username, String comment, String p_o_i) {
this.lat = lat;
this.lng = lng;
this.username = username;
try {
this.id = IdBroker.getId(new DataSource().getConnection());
} catch (PersistenceException e) {
e.printStackTrace();
}
time_stamp = Util.timeStamp();
this.comment = comment;
this.point_of_interest = p_o_i;
}
public Hotspot(double lat, double lng, String username, long id, String time_stamp) {
this.lat = lat;
this.lng = lng;
this.username = username;
this.id = id;
this.time_stamp = time_stamp;
}
//Getter Setter for all variable and HashCode/Equals
}
And this is the subcalss
package persistence.postgres;
import elements.Details;
import elements.Hotspot;
public class HotspotProxy extends Hotspot {
private HotSpotDAOpostgres hsd;
private Details details;
//insert
public HotspotProxy(double lat, double lng, String username, String comment, String poi) {
super(lat, lng, username, comment, poi);
this.hsd = new HotSpotDAOpostgres();
}
//Retrieve
public HotspotProxy(double lat, double lng, String username, long id, String time_stamp) {
super(lat, lng, username, id, time_stamp);
this.hsd = new HotSpotDAOpostgres();
}
public String getPointOfInterest() {
redifined method
}
public String getComment() {
//redifined method
}
}
and this give me error
Hotspot h = new Hotspot(31.124, 43.123, "name", "comment", "point of interest");
HotspotProxy hp = (HotspotProxy)h;
Please tell me where im failing. thanks for any suggention
Upvotes: 2
Views: 7261
Reputation: 7846
Sometimes a object variable is in fact an instance of a subclass of whatever the variable is. For example, you could have
Hotspot h = new HotspotProxy(...)
because a HotspotProxy
is a Hotspot
. In that situation the boolean (h instanceof HotspotProxy)
would be true
. However in your case, the h
variable is Hotspot
so (h instanceof HotspotProxy)
would be false
and the cast fails.
Upvotes: 1
Reputation: 1
You can downcast, provided that the reference variable you are using actually points to a subclass object. For example, let's say class Dog extends class Animal.
Then
Animal a = new Dog();
Animal b = new Animal();
Dog d1=(Dog)a; //this works
Dog d2=(Dog)b; //this fails at run time
The compiler will trust us when we cast this way: all it ensures is that the objects belong to same inheritance hierarchy. So this will compile fine. But in the second case here, as well as in yours, this throws ClassCastException be.
You can check your code before downcasting to prevent such errors:
if(HotSpot1 instanceof HotSpotProxy)
{
HotSpotProxy hsp= (HotSpotProxy) HotSpot1;
}
Upvotes: 0
Reputation: 49402
It is because you are trying to cast a Super Class object to Sub Class.
Hotspot h = new Hotspot(31.124, 43.123, "name", "comment", "point of interest");
HotspotProxy hp = (HotspotProxy)h;
You had done a type-coercion in the second statement and fooled the compiler in believing that h
is indeed a HotspotProxy
object . Compiler allows this because Hotspot h
can refer to any object of its subclass. So if the previous line was Hotspot h = new HotspotProxy()
, this cast would have succeeded .But at run time JVM finds that you have violated the trust of the compiler and h
is an object of Hotspot
, not HotspotProxy
and hence it throws the exception.
Upvotes: 1
Reputation: 967
Check first if HotSpot is instance of hostspotproxy
if(h instanceof HotspotProxy)
HotspotProxy hp = (HotspotProxy)h;
Upvotes: 0
Reputation: 37823
Hotspot := Animal
HotspotProxy := Dog
You can cast a Dog
to an Animal
(from special to general, or in other words from subclass to baseclass), but not the other way around (from general to special, or in other words from baseclass to subclass), because every Dog
is an Animal
, but not every Animal
is a Dog
.
If you had something like
Animal a = new Dog();
then, the following would be allowed:
Dog d = (Dog) a;
because a
is actually a Dog
.
Upvotes: 7
Reputation: 95988
You can't cast an instance of a superclass to a subclass.
That's right that HotspotProxy
is also an Hotspot
, but.. not all Hotspot
s are HotspotProxy
s.
Note that this is not a compilation error. It's a run-time error. That's because you did the cast (explicit cast!), so basically you told the compiler to trust you that you know what you do, but.. you don't. So the compiler will say "OK, I'll compile, he knows what he's doing", but when you run the program, you'll get a run-time error.
(If you remove the cast, you'll indeed get a compilation error).
Upvotes: 2