Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding a CampFire Story Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarding a CampFire Story" Watch "Question regarding a CampFire Story" New topic
Author

Question regarding a CampFire Story

Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30
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

Joined: Aug 26, 2001
Posts: 7610

"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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 128
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).


Darryl Failla
Sun Certified Java 2 Programmer
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi,
will this be right
Object o=b; Observer o2 =(Observer)o;
Observer o=new B();
Regards
Neha

nss
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 13, 2001
Posts: 30
(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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Nov 13, 2001
Posts: 30
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

Joined: Nov 13, 2001
Posts: 30
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

Joined: Aug 26, 2001
Posts: 7610
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question regarding a CampFire Story
 
Similar Threads
casting rules-ques from jq+
casting
Casting
interface again
Interface and Casting