File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from Dan's exam

 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic