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

class casting

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}



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


the answer is one.......if anybody has understood kindly explain....Thanks in advance
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 1 and 4.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from where are you getting such interesting questions


well the answer should be 1
after applying is-a relationship you can do it
like B is-a A,E
C is-a A
D is-a B means D is also A,E

and when we perform a cast only the object is used not the reference
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 is answer becuse at runtime the object type is taken into consideration not the reference type, so at runtime jvm sees it as object D being casted to E and d implements E through its superclass so no exceptions will be there.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At line 4--> we are casting object a with (E). a is actually D (at runtime). So a require casting as class D is subclass of class B which implements interface E.
At line 5 -->we are casting e with B because downcasting is required.

Answer should be 1.
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:1 is answer becuse at runtime the object type is taken into consideration not the reference type, so at runtime jvm sees it as object D being casted to E and d implements E through its superclass so no exceptions will be there.



Answer 4 says that in line 4 the (E)a cast is required. And it is as the code doesn't compile without it (try it yourself). It shouldn't because in Java type safety is checked at compile time. Here we're trying to assign reference of type A to reference of type E. Class A doesn't implement interface E so this assignment requires explicit cast for the compiler. In runtime everything is fine - it's object of type D which is being cast to E, and D implicitly implements E, so no exception. That's why correct answers are 1 and 4.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic