aspose file tools*
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
Author

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;
class.method();
This allows you to access a member of the class you're casting to in one statement.
The statement:
(Class)Object.method();
says: cast the return value from Object.method() to type Class,
which is entirely different from:
((Class)Object).method();
which says: cast Object to type Class and invoke the method() member.
hth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q from Dan's exam