Link to home
Start Free TrialLog in
Avatar of krakatoa
krakatoaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

What's the explanation for this not compiling ?

What's the explanation for this not compiling ?

SSLSocketFactory factory = SSLSocketFactory.getDefault();

from, Java in a Nutshell, Flanagan, 5th edition, page 948.

?

Error reads :

: error: incompatible types: SocketFactory cannot be converted
o SSLSocketFactory
Avatar of Darren
Darren
Flag of Ireland image

Hi,

The class is a SSLSocketFactory is static so...

SocketFactory factory = SSLSocketFactory.getDefault();

Open in new window


Thanks,

Darren
Or maybe....


SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

Open in new window

Avatar of krakatoa

ASKER

I know it’s static.
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.getDefault() has a return type of SocketFactory. You cannot directly assign an object of type  SocketFactory to a SSLSocketFactory variable without casting.
This is implied in the comments above
No - it's stated
OK, I understand that, and in fact 'my' code has been all along :

SocketFactory ssl_s_f = (SSLSocketFactory)SSLSocketFactory.getDefault();

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.getDefault();
SSLSocket securesock = (SSLSocket)factory.getSocket(hostname, 443); //https port "

As an aside, I can't trace where the method 'getSocket(host,port)' originates from either. Any ideas ?

This is implied in the comments above
No - it's stated

You are correct. it was stated explicitly. Not sure how I missed that initially. My apologies.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
User generated image
:)
I tried to check errata to no avail
Right. The read-only language of print strikes again. ;)