0

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);
Jonathan
  • 3,016
  • 9
  • 43
  • 74

5 Answers5

0

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

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

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.

John Kane
  • 4,383
  • 1
  • 24
  • 42
  • 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
0

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

0

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.

TMN
  • 3,060
  • 21
  • 23
0

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

obam
  • 1