File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from Dan's exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from Dan Watch "Q from Dan New topic

Q from Dan's exam

Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
The ques is
interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {
String s1 = "C";
void printIt() {
System.out.print(((A)this).s1 + ((B)this).s1 +
((C)this).s1 + ((I)this).s1);
public static void main (String[] args) {
new C().printIt();
The ans is ABCI.
I am a bit lost in the SOP line in the class C. How should I interpret this type cast?
Please advice.

Thanks,<br />Lakshmi.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
The ((Class)Object).method() syntax is a shortcut for:
Class class = (Class)Object;
This allows you to access a member of the class you're casting to in one statement.
The statement:
says: cast the return value from Object.method() to type Class,
which is entirely different from:
which says: cast Object to type Class and invoke the method() member.
I agree. Here's the link:
subject: Q from Dan's exam
It's not a secret anymore!