• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Q about interfaces: implicit conversion

 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
In a quiz I got the question: Implicit converting an interface
to a class type is never allowed. (TRUE or FALSE?)
I think it's TRUE, because classes always implement interfaces
and therefore only a class can be seen as IS-A interface. But not
the other way round.
The answer though is false and says: an interface type can be converted
to a class type, only if the class implements the interface.
I think this is right only if in the question it hadn't explicity
mentioned the word 'implicit'.
Am I right?
Best regards,
Gian Franco Casula
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's TRUE, because classes always implement interfaces
and therefore only a class can be seen as IS-A interface. But not
the other way round.

This the design/implementation part.
The answer though is false and says: an interface type can be converted
to a class type, only if the class implements the interface.
I think this is right only if in the question it hadn't explicity
mentioned the word 'implicit'.
Am I right?

This statement talks about variable types, not about the interface
or class. You seem to be mixing concepts here.....
The mention of the word [i]implicit[/b] in the question is valid, IMO.
To me it means that you don't have to type-cast.
Other comments welcome....
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO the answer is TRUE. An interface type cannot be implicitly converted to a class-type.
For e.g. code below will not compile

U get a compile error on line "implementor = interfac;" But it would compile if changed to "implementor = (Implementor)interfac;"
Am I right?
Bijesh
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bijesh Krishnadas:

Am I right?


That is correct. A reference to an interface is never implicitly cast as an implementing class. One possible reason for this might be the fact that multiple classes might implement the same interface. Therefore, it is impossible to know which class to cast the object as.
You can explicitly cast an object, but it is never done implicitly in this frame of reference.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errrrr......one bartender said the answer is FALSE (to use the IMPLICIT in that reference) but another bartender said it is TRUE (as interface is never implicitly cast as an implementing class). I am a bit confused. Could someone please explains this further. Thank you.
Cheers.
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no implicit conversion of
> Interface type into implementing class
nor of
> super-class type into sub-class.
Explicit conversion will compile but runs the risk of ClassCastException if improper casting is done
Bijesh
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
coming back to my original question.
The authors of the quiz reformulated the question:
Question: Converting an interface type to a class type is never allowed.
Options:
True
False
Correct Answer:
B
Explanation.
An interface type can be converted to a class type, only if the class implements the interface.
A reference to an interface is never implicitly cast as an implementing class.

Best regards,
Gian Franco Casula
 
He does not suffer fools gladly. But this tiny ad does:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic