Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object Casting

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following answers are correct n y???
----------------------
class AA extends Object implements I1
{
}
class BB extends AA implements I2
{
}
interface I1
{
}
interface I2
{
}
--------
Given
AA a = new AA();
BB b = new BB();
------------
OPTIONS
1)Object o = a;
I1 i1 = o;
2)Object o = a;
I1 i1 = (I1)o;
3)Object o = a;
I2 i2 = (I2)a;
4)Object o = b;
I2 i2 = o;
---------
GUNJAN
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 2.
1) 4)are wrong because explict casting is required in down casting
3) is wrong because a is a object of class AA, which does not implement I2
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gunjan kuwadia:
Which of the following answers are correct n y???


Can u explain me???
GUNJAN
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for detailed explanation, you can refer to Chapter 4 of the Heller book
------------------
Hima
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gunjan
remember two main facts abt casting
1.At compile time,it will check if the two objects can denote an object of a class,where this class is subtype of both source and destination.
Otherwise it will give compile time error.
2.if it compiles then at run time it will check for the cast.
suppose the cast is
(abc)dest
if dest instanceof abc returns true,then it is a valid cast otherwise it will give class cast exception.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic