Title: How to Type cast the classes in Android?
Tags: java;android
Question: I have a class Like
Class:StatusEventChangeDto75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0
public class StatusEventChangeDto {
protected String address;
DutyState status;
protected double latitude,longitude;
protected long time;
public StatusEventChangeDto()
{}
public StatusEventChangeDto(double latitude, double longitude, String address, long time, DutyState status)
this.latitude = latitude;
this.longitude = longitude;
this.address = address;
this.time = time;
this.status = status;
public String getAddress() {
return address;
public void setAddress(String address) {
this.address = address;
public long getTime() {
return time;
public void setTime(long time) {
this.time = time;
public DutyState getStatus() {
return status;
public void setStatus(DutyState status) {
this.status = status;
public double getLatitude() {
return latitude;
public void setLatitude(double latitude) {
this.latitude = latitude;
public double getLongitude() {
return longitude;
public void setLongitude(double longitude) {
this.longitude = longitude;
@Override
public String toString()
StringBuffer sb = new StringBuffer();
sb.append("Lat : "+latitude+"\n");
sb.append("Long : "+longitude+"\n");
sb.append("Address : "+address+"\n");
sb.append("Time : "+time+"\n");
sb.append("Status : "+status+"\n");
return sb.toString();
And one More class as
Class:StatusEventDurationChangeDto
public class StatusEventDurationChangeDto extends StatusEventChangeDto
private Duration duration;
public StatusEventDurationChangeDto()
public Duration getDuration() {
return duration;
public void setDuration(Duration duration) {
this.duration = duration;
public String toString() {
StringBuffer sb = new StringBuffer(super.toString());
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendHours()
.appendSuffix("h")
.appendMinutes()
.appendSuffix("m")
.toFormatter();
String formatted = formatter.print(duration.toPeriod());
sb.append("Duration : "+formatted+"\n");
return sb.toString();
Here I am Type Caste the Two above class Like this
StatusEventChangeDto event1 = new StatusEventChangeDto(17.45, 78.45, "hyderabad, Telangana", now.minusHours(1).getMillis(), ONDUTY);
StatusEventChangeDto event2 = new StatusEventChangeDto(17.48, 78.50, "hyderabad, Telangana", now.minusHours(2).getMillis(), OFFDUTY);
StatusEventDurationChangeDto duration1 = (StatusEventDurationChangeDto)event1;
duration1.setDuration(new Duration(now, now.minusHours(1)));
StatusEventDurationChangeDto duration2 = (StatusEventDurationChangeDto) event2;
duration2.setDuration(new Duration(now, now.minusHours(2)));
When i run this i got Type cast Exception
Help Me Guys
Comment: Can you please your complete code here? I can not see extended class.

Comment: you are trying to cast parent class in child class, and your parent class is not extending class in which you are casting. Try to cast child class in parent and it will not give you any error.

Comment: hope this will help you to understand casting in java
http://stackoverflow.com/questions/2233902/java-rules-for-casting

Comment: This is basic Java. It has nothing to do with Android. You simply can't cast an object to a type it doesn't extend and isn't its own type. `StatusEventChangeDto` doesn't extend `StatusEventDurationChangeDto`

Comment: How can I write this one?


Here is another answer: You can't cast event1 and event1 to StatusEventDurationChangeDto because this varaibles has link to instance of object StatusEventChangeDto which not extends StatusEventDurationChangeDto it's vice versa.

Here is another answer: You have
StatusEventDurationChangeDto extends StatusEventChangeDto
which means that StatusEventDurationChangeDto is a StatusEventChangeDto so you can cast StatusEventDurationChangeDto to StatusEventChangeDto  but you can not cast StatusEventChangeDto to StatusEventDurationChangeDto.
The general rule is that you can cast something down in the hierarchy to something up but not the other way around.

Here is another answer: the problem is here:
StatusEventDurationChangeDto duration1 = (StatusEventDurationChangeDto)event1;
You can not cast the objects of event1/2 to StatusEventDurationChangeDto as event1/event2 are of the type of StatusEventChangeDto (your own defined class)
I think you should consider the class as a "USER DEFINED DATA TYPE". You should first take the full concept of class and type casting.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/07/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android>', 'Title': 'How to Type cast the classes in Android?', 'ViewCount': 1098, 'id': 29492429, 'user_AboutMe': None, 'user_AccountId': 4809363.0, 'user_DisplayName': 'Manikanta Reddy', 'user_Reputation': 603.0, 'user_Views': 149.0, 'user_id': 3883280.0}}