View Full Version : Java: converting an inherited type to a descendent type

February 15th, 2012, 10:20 PM
Nevermind, I found the answer in the DataOutputStream constructor.

Ok, I'll be straight up, I'm working on homework, HOWEVER, this question is not about the assignment in particular, but something I'm having trouble with. In the homework, we're supposed to be working on a webserver that processes HTTP requests, and we're doing so in java. The only problem is, I haven't touched java for three semesters and thus am still relearning it. I've been hugging onto the java documentation for dear life, but I seem to be stuck on something.

As part of the assignment, I need to get a DataOutputStream (http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html) class from a Socket (http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#getOutputStream()). Unfortunately, the closest I can get from Socket is the OutputStream (http://docs.oracle.com/javase/1.4.2/docs/api/java/io/OutputStream.html). Now, DataOutputStream inherits OutputStream, so I don't see any reason I can't cast OutputStream to DataOutputStream in the program.. The problem is, I can't remember how to do that or if java will even let me. If anyone could point me in the right direction, I would really appreciate it. Thanks.

DataOutputStream os = mainSock.getOutputStream();