• 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

casting rules-ques from jq+

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
please look to the following code
class A implements Runnable{}
class B extends A implements Observer{}
and the dec
A a=new A();
B b=new B();
which of the foll will compile and execute without throwing exceptions
1)Object o=a;Runnable r=o;
2)Object o=a;Runnable r=(Runnable) o;
3)Object o=a;Observer ob=(Observer)o;
4)Object o=b;Observer o2=o;
5)Object o=b;Runnable r=(Runnable)o;
ans are 2 and 5.why not 3 as well??
please explain because i think ,in case of 1) and 2)when a implements Runnable then what is the need of explicit casting.does assingning objects to another Object ref makes a diff???
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Object o=a;Runnable r=o;
o is a normal Object which must explicitly cast into a Runnable.
2)Object o=a;Runnable r=(Runnable) o;
Appropriately cast, hence compiles & executes.
3)Object o=a;Observer ob=(Observer)o;
Compiles due to appropriate casting, but since A does not implement Observer, fails to run.
4)Object o=b;Observer o2=o;
Must be cast to Observer.
5)Object o=b;Runnable r=(Runnable)o;
B does not implement Runnable

Please correct if any of the above reasonings is wrong.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Object o=a;Runnable r=o;
o is a normal Object which must explicitly cast into a Runnable.
2)Object o=a;Runnable r=(Runnable) o;
Appropriately cast, hence compiles & executes.
3)Object o=a;Observer ob=(Observer)o;
Compiles due to appropriate casting, but since A does not implement Observer, fails to run.
4)Object o=b;Observer o2=o;
Must be cast to Observer.
5)Object o=b;Runnable r=(Runnable)o;
B does not implement Runnable

I think 5 should be ok since B is extending A which implements Runnable .Correct me if i am wrong.
 
Kumar Mahesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Object o=a;Runnable r=o;
o is a normal Object which must explicitly cast into a Runnable.
2)Object o=a;Runnable r=(Runnable) o;
Appropriately cast, hence compiles & executes.
3)Object o=a;Observer ob=(Observer)o;
Compiles due to appropriate casting, but since A does not implement Observer, fails to run.
4)Object o=b;Observer o2=o;
Must be cast to Observer.
5)Object o=b;Runnable r=(Runnable)o;
B does not implement Runnable

I think 5 should be ok since B is extending A which implements Runnable .Correct me if i am wrong.
 
S Dave
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's right. option 5 compiles and executes as B extends A which implements Runnable.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dengri,
third option is wrong because it is the class A which implicitly extends extends class Object and implements runnable.
Now the point is class A implements Runnable not Object class.so once u assign the reference of class A to class Object then u have to explicitly typecast the Object class varaible to the Runnable type.Now it is the typecasting in the opposite direction.now see the figure below.
A-------extends Object---------implements Runnable.
this is probably what is going on in your mind.The actual thing is like this.
1) A------extends Object.
2) A--------implement Runnable.
Now see statement (1)once u assign an object of class A to object , it is assigned becuase of up the heirarchy order.
Now see statement (2)It is class A which implement's Runnable not the Class Object.so till now your Object class varaible has the reference of Class A,u have to come down from object to Runnable, therefore u need an explixit typecasting.
Remember it is the class A which implmenet's Runnable not the Object.

Now please tell me one thing.In the line given below whether the class A implicitly extends class Object or not.

class B extends A implements Observer{}
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi nitin,
u cud guess right!! i was confused coz of assigning to an Object ref ,now it is clear.as far as ur ques about
In the line given below whether the class A implicitly extends class Object or not.
class B extends A implements Observer{}
it does implicitly extends Object.Correct???
but now see this one which does not involve a third ref to Object type
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
why this code is giving runtime classCast Exception???
Acc to me a ref of superclass type can be explicitly casted to ref of subclass type.it is this what is happening.
please explain.
thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic