krakatoa
asked on
What's the explanation for this not compiling ?
What's the explanation for this not compiling ?
SSLSocketFactory factory = SSLSocketFactory.getDefaul t();
from, Java in a Nutshell, Flanagan, 5th edition, page 948.
?
Error reads :
: error: incompatible types: SocketFactory cannot be converted
o SSLSocketFactory
SSLSocketFactory factory = SSLSocketFactory.getDefaul
from, Java in a Nutshell, Flanagan, 5th edition, page 948.
?
Error reads :
: error: incompatible types: SocketFactory cannot be converted
o SSLSocketFactory
Or maybe....
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
ASKER
I know it’s static.
The question is why it doesn’t compile.
The question is why it doesn’t compile.
You can't assign a supertype to a subtype without casting. getDefault returns SocketFactory which is the supertype of SSLSocketFactory
This is implied in the comments above, but just to make it explicit:
SSLSocketFactory.getDefaul t() has a return type of SocketFactory. You cannot directly assign an object of type SocketFactory to a SSLSocketFactory variable without casting.
SSLSocketFactory.getDefaul
This is implied in the comments aboveNo - it's stated
ASKER
OK, I understand that, and in fact 'my' code has been all along :
SocketFactory ssl_s_f = (SSLSocketFactory)SSLSocke tFactory.g etDefault( );
But what is the reason the book has it down as posted in my first comment?The context on the page is this :
"Clients that want to perform simple SSL-enabled networking can create an SSLSocket with code like the following :
SSLSocketFactory factory = SSLSocketFactory.getDefaul t();
SSLSocket securesock = (SSLSocket)factory.getSock et(hostnam e, 443); //https port "
As an aside, I can't trace where the method 'getSocket(host,port)' originates from either. Any ideas ?
SocketFactory ssl_s_f = (SSLSocketFactory)SSLSocke
But what is the reason the book has it down as posted in my first comment?The context on the page is this :
"Clients that want to perform simple SSL-enabled networking can create an SSLSocket with code like the following :
SSLSocketFactory factory = SSLSocketFactory.getDefaul
SSLSocket securesock = (SSLSocket)factory.getSock
As an aside, I can't trace where the method 'getSocket(host,port)' originates from either. Any ideas ?
This is implied in the comments aboveNo - it's stated
You are correct. it was stated explicitly. Not sure how I missed that initially. My apologies.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It's a mammoth blunder by the author imo. If he's referring to (an)other API he certainly isn't sharing the fact with his readers.
Wonder if the text is online. Can you quote something googleable from it?
:)
I tried to check errata to no avail
I tried to check errata to no avail
ASKER
Right. The read-only language of print strikes again. ;)
The class is a SSLSocketFactory is static so...
Open in new window
Thanks,
Darren