What's the explanation for this not compiling ?

krakatoa
krakatoa used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DarrenSenior Software Engineer

Commented:
Hi,

The class is a SSLSocketFactory is static so...

SocketFactory factory = SSLSocketFactory.getDefault();

Open in new window


Thanks,

Darren
DarrenSenior Software Engineer

Commented:
Or maybe....


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

Open in new window

I know it’s static.
The question is why it doesn’t compile.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Top Expert 2016

Commented:
You can't assign a supertype to a subtype without casting. getDefault returns SocketFactory which is the supertype of SSLSocketFactory
Jan LouwerensSoftware Engineer

Commented:
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.
Top Expert 2016

Commented:
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 ?
Jan LouwerensSoftware Engineer

Commented:

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.
Top Expert 2016
Commented:
But what is the reason the book has it down as posted in my first comment?
Probably an error. Unless he's using a different library

I can't see getSocket either. Perhaps he IS using a 3rd party library

Not sure how I missed that initially. My apologies.
np
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.
Top Expert 2016

Commented:
Wonder if the text is online. Can you quote something googleable from it?
Top Expert 2016

Commented:
:)
I tried to check errata to no avail
Right. The read-only language of print strikes again. ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial