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
LVL 17
krakatoaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DarrenSenior Software EngineerCommented:
Hi,

The class is a SSLSocketFactory is static so...

SocketFactory factory = SSLSocketFactory.getDefault();

Open in new window


Thanks,

Darren
0
DarrenSenior Software EngineerCommented:
Or maybe....


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

Open in new window

0
krakatoaAuthor Commented:
I know it’s static.
The question is why it doesn’t compile.
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

CEHJCommented:
You can't assign a supertype to a subtype without casting. getDefault returns SocketFactory which is the supertype of SSLSocketFactory
0
Jan LouwerensSoftware EngineerCommented:
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.
0
CEHJCommented:
This is implied in the comments above
No - it's stated
0
krakatoaAuthor Commented:
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 ?
0
Jan LouwerensSoftware EngineerCommented:

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.
0
CEHJCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaAuthor Commented:
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.
0
CEHJCommented:
Wonder if the text is online. Can you quote something googleable from it?
0
krakatoaAuthor Commented:
Nutshell SSL
0
CEHJCommented:
:)
I tried to check errata to no avail
0
krakatoaAuthor Commented:
Right. The read-only language of print strikes again. ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.