• 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

Doubt in Casting question, from uCertify

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base
{
public void fun(){ System.out.println("Fun");
}
}

clss RefCast extends Base
{
public static void main(String args[])
{
RefCast obj = (RefCast)new Base(); //9
obj.fun();
}
}

A. It will compile and execute but no output will be displayed
B. A runtime exception occurs
C. A compile-time error will occur
D. It will compile and execute successfully and will display the output as Fun

Correct Answer: B
Can anyoen explian why the answer is B and not D
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anyoen explian why the answer is B and not D



Well, it would help if you explain to us why you believe that it should be D and not B.

Henry
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer cannot be b. an object of a super class cannot be casted into an object of a sub-class....

lets take this example


Now the object is of type Super. If no exception occurs at run-time, then it will try to access b on the object. But the object has only one instance variable i.e. a. So when we try to access b, it is not a part of Super. This is why the cast produces an exception....But the opposite can be done...

Super sup = new Sub();
sup.a = 20; //OK
sup.b = 30; //Error

The last statement generated an error as you can't call a member of class Sub on a reference of type Super. Remember that there is a difference between object and reference.

sup = new Sub();

here sup is the Reference and it denotes an Object of type Sub....
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit. Its very clear now.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic