File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object orientation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object orientation" Watch "Object orientation" New topic
Author

Object orientation

harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
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() {

\\ 1 System.out.print(((A)this).s1 + ((B)this).s1 +
((C)this).s1 + ((I)this).s1);
}
public static void main (String[] args) {new C().printIt();}
}

can anyone explain wats happening in line 1
step by step how the parenthesis gets evaluated at line 1
and the output


Harish<br />SCJP 1.4 (85%)
Raj Man
Greenhorn

Joined: Feb 28, 2006
Posts: 1
In each of class ie A, B and C definition, you are defining same member variable. When you assign member variable at each class level, it's only reflected at that class level member variable and does not change value at super level.

When you print values with narrow typecasting, there is a value available for s1 at each narrowed scope of object, which is printed (ABCI). Assume that you do not have values defined at B and C level, the output would be "AAAI" (refer code below). It's good question I would say.

interface I {String s1 = "I";}
class A implements I {String s1 = "A";}
class B extends A {}
class C extends B {

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();}
}
Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 69
I thought all variables declared in an interface are public static final (as said in K&B scjp 5 exam guide pg23)and that its value cannot be reset by a class which implements it. The example mentioned here seems to prove my assumption wrong.

Pls clarify...


Thanks<br />Rajesh
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
The interface member variable by defaulty public,static,final.

It is public because it has to access out side the interface.
It is final because the value never changes
It is static because you can never have an instance of an interface (that isn't an instance of a class). Where would the instance variable live?


java j2ee job interview questions with answers | Learn the core concepts and the key areas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object orientation
 
Similar Threads
Doubt in an Interface Question!!
Interface Question
Q from Dan's exam
interface rule
interface