• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Casting and Interfacses

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all


I'm not able to understand it
Plz explane me...

Tq
GMP
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its option 1 , correct me if am wrong.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh,

The ans B is wrong as forward referencing is not in java this code will compile fine the ans should be A) and D).

Gaurav
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be A & D. It doesnt matter what order the interface is declared in.It could even be in another file.
Order does matter with variables in a class. For example you couldnt say this:
=========
int x = b + c;
int b = 1;
int c = 3;
==========
The casts on both line 4 and 5 are mandatory.On 4 because A doesnt directly implement E. Try E e = new B(); -> This is fine because B directly implements the interface E. But at run time a really is a D so no runtime error. If a was an A = runtime error.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Option A is correct : No compile or runtime errors come out.

Options B and C is not correct due to above.

Option D is correct : Although (a) contains a reference to class type D, the compiler doesn't know that. The compiler sees (a) as type of class A, so the cast is mandatory here in otder to allow the code compile.

Option E is incorrect: Same explanation as above.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont get why the answer is not C...the rules of object reference casting in Philip Heller/Simon Roberts book says,"when castin one class to another or one interface to a class,one of them should be a subclass or implement the other in order for the code to compile ".Here in the example i dont see a chance of it compiling 'cos A does not implement E but has been cast to E,which is wrong.Anyone who is really sure abt this concept,pls help.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vidhya,

I dont get why the answer is not C...

Quote from JLS2 - 5.5:

---------------
The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
  • If S is a class type:
  • If T is an interface type:
    If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
    ---------------

    In the sample code posted by Ganesh, class A is the S and interface E is the T. Since class A is not final, it can be extended and its subclasses may implement E.

    For example:


    Example with class A being declared as final:


    Example with E as a class:


    Joyce
     
    If you want to look young and thin, hang around old, fat people. Or this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic