• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt with conversion

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source http://www.valoxo.ch/jr/mocks/mock01a.html Q7

Select two correct statements about the code given below?
class A{}
class B extends A implements E{}//line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
public static void main(String[] args) {
A a = new D();//line 2
C c = new C();//line 3
E e = (E)a;//line 4
B b = (B)e;//line 5
}
}

A. The code compiles without error and runs fine.
B. Compilation error on line 1 because interface E is not yet declared (forward-referencing).
C. Compilation error on line 4 because class A does not implement interface E.
D. The cast on line 4 is mandatory.
E. The cast on line 5 is not mandatory.

Ans: A,D
Doubt: is line 4 valid as there is no relation between A and E. Also does the runtime class matters or the compile time?

pls help,
Gitesh
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is OK. The cast is mandatory because the compiler doesn't know that the object does implement E, you do. It will run because you know that the instance at 'a' actually implements E. You would get an invalid class cast runtime exception if it doesn't.
By using the cast you are taking the responsibility of assuring that the cast is valid by having control of which object is instantiated there. The compiler can't guarantee it will be a valid cast, it only knows it is possible that the cast will be valid and you should make sure it is.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg you mentioned:

instance at 'a' actually implements E



But A does not implement E. It is D that implements E.

I'm still confused.


Gitesh
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, but you know that at 'a' you're holding a D, which is an A and an E too.
If A had implemented E in the first place, you wouldn't need a cast to make that assignment.
The compiler only knows that 'a' is just an A, but it can actually be an instance of a subclass that does implement the interface E.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it is allowed that its always possible to cast to interface type because even the class itself doesnt implement the interface a sub class might, with exception that the class itself is marked final.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg, does it mean that everything depends on runtime object, not the reference type.

Please correct me if I got it wrong.

Gitesh
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, "everything", if you mean what goes on here, you could put it that way... what you have to understand is that it is possible to hold an E with a reference type A, considering subclasses. That's why the compiler lets you do it. If at runtime that is not true, the cast will blow up your app.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic