Take the 2-minute tour ×

I'm trying to implement a private message system. Let me know if this is bad design but I have two classes User and Recipient. Recipient is a User so it inherits User. Recipient has additional properties like messageId, readDate, keepMessage.

My code is as follows:

//This line gives me ClassCastException
recipient = (Recipient) user;


 //GET id of user to send message to
String receiverId = request.getParameter("id");

//GET title of message
String title = request.getParameter("title");

//Get content of message
String content = request.getParameter("content");

//Retrieve logged in user from session
HttpSession session = request.getSession();     
User sender = (User) session.getAttribute("user");

//Instantiate a new User to hold receiver
User user = new User();
//Retrieve object of user to send message to
UserService userService = new UserService();
user = userService.getUserById(Integer.valueOf(receiverId));

//Instantiate a new Recipient (extends User)
Recipient recipient = new Recipient();
//Cast User as a Recipient
recipient = (Recipient) user;

//Instantiate a message
Message message = new Message();
//message related stuff here....

//Pass the message content and Recipient to messageService
MessageService messageService = new MessageService();
messageService.sendPrivateMessage(message, recipient);
share|improve this question
I don't think this will compile. You define the variable recipient twice –  ControlAltDel Apr 13 '12 at 18:45
show user class syntax and Recipient class syntax. –  Nambari Apr 13 '12 at 18:47
Thanks @user1291492 I fixed the example. –  Jonathan Apr 13 '12 at 18:47

5 Answers 5

up vote 0 down vote accepted

You cannot really do that with Java. Based on what you said above you cannot cast a Recipient to a User. Do you really need to do this though? In the instance above it looks like you should be able to just instantiate it as a Recipient.

share|improve this answer
How is it possible to instantiate userService.getUserById(Integer.valueOf(receiverId)); as a Recipient when it returns a User? –  Jonathan Apr 13 '12 at 18:55
Are there different types of Users other than a Recipient? What I am thinking is that you could possibly make User an abstract class (or interface depending on what can be shared). –  John Kane Apr 13 '12 at 18:59
No there isn't. I'm wondering should I have a Recipient class at all? Instead of passing in a Recipient to messageService.sendPrivateMessage(message, recipient); should I just pass in user.getUserId()? –  Jonathan Apr 13 '12 at 19:04
Or what about still passing in a Recipient but don't inherit from a User and just set the parameters? e.g. recipient.setUserId(user.getUserId) ? –  Jonathan Apr 13 '12 at 19:06
If you instantiate the Object as a Recipient you should be fine, but I am not entirely sure that there needs to be a distinction between the two though. –  John Kane Apr 13 '12 at 19:13

Aside from your class hierarchy, which is another issue, you have multiple syntax problems. As I pointed out you are defining the recipient variable twice, with 2 different classes. Also, it looks like you are trying to cast a User to a Recipient, which will fail - a recipient is a user, but a user isn't necessarily a recipient

share|improve this answer

You cannot cast a super class to a derived one. Where should the additional subclass data come from?

share|improve this answer

You can't do it with a cast. Your best bet is to add a constructor to Recipient that takes a User argument. Then you can do

Recipient recipient = new Recipient(user);

Either that or change sendPrivateMessage() to accept a User instead of a Recipient.

share|improve this answer

I think it is not good to do downcast, please check your class hierachy. if that object is really a child object, you can use dynamic_cast

share|improve this answer

Your Answer


