Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding a CampFire Story

 
Roger Chan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a code snippet:
Consider the following classes:
class A implements Runnable{ ...}
class B extends A implements Observer { ...}
and the declarations :
A a = new A() ;
B b = new B();
Which of the following Java code fragments will compile and execute without throwing exceptions?
(a)Object o=a; Runnable r = o;
(b)Object o=a; Runnable r = (Runnable)o;
(c)Object o=a; Observer ob = (Observer)o;
(d)Object o=b; Observer o2 = o;
(e)Object o=b; Runnable r = (Runnable)b;
The correct answer is b and e. I understand the concept of using object reference to its sub-class object (e.g. Object o=a). One of the JavaRanch CampFire stories explain it really well. But I am not quite sure about this:
"If the reference type is an INTERFACE, the object it refers to MUST be an object from a class which implements the interface (either directly or through inheritance)." -- Quote from How my Dog learned Polymorphism, JavaRanch
Can anybody illustrate the above concept using the code snippet above? Thanks
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"If the reference type is an INTERFACE, the object it refers to MUST be an object from a class which implements the interface (either directly or through inheritance)." -- Quote from How my Dog learned Polymorphism, JavaRanch

Runnable is an interface. Let's consider the following code snippet:

"a" is a variable of type A (and Object and Runnable as well since class A implicitely extends Object and implements interface Runnable). That means that the object referenced by variable a, can also be referenced by variables of type Object and Runnable like it is the case on the two last lines of the code above.
The line before the last one declares a variable "o" of type Object, which means that "o" can reference whatever we want to. But by doing this, the object that is referenced by "o" is of type Object. Here the instance of A is referenced by the variable "o" of type Object. Now the last line tries to convert the type of the object referenced by "o" and thus a cast is required from Object to Runnable. But to do this we must be sure that the object (i.e. the instance of A) actually implements Runnable otherwise a ClassCastException will be thrown...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the example, A can be cast as Object(universal inheritance), A(declaration), or Runnable(interface implementation). Since B extends A, B inherits all of these from A. Therefore, B can be cast as Object(universal inheritance), B(declaration), Observer(interface implementation), A(inherit from A), or Runnable(inherit from A).
As noted in the above explanation, explicit casts are needed in answers b and e because objects a and b are first upcast to Object (no explicit cast needed). From there, the path to Runnable is a downcast (need explicit cast).
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
will this be right
Object o=b; Observer o2 =(Observer)o;
Observer o=new B();
Regards
Neha
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha,
yes the code you provide is correct since B implements Observer.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Roger Chan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(e)Object o=b; Runnable r = (Runnable)b;
The second statement is doing upcasting it. Isn't it? Since Object B inherits all members from Object A. Do we still need an explicit casting (Runnable)?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is necessary because o is a variable of type Object and not B.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[This message has been edited by Valentin Crettaz (edited November 26, 2001).]
 
Roger Chan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin,
Object o=b; Runnable r = (Runnable)b;
Do you mean that object reference b is converted to Object type in the first statement? Since b is no longer in "b" type in the second statement, casting is required.
Thanks
 
Roger Chan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object o=b; Runnable r = (Runnable)b;
Does the first statement mean that object reference b is converted to Object type? Since b is no longer in "b" type in the second statement, casting is required.
Thanks
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Roger.
In the first statement the reference to object b is converted to a reference of type Object and thus the reference to b is no longer of type B as when it was created.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic