• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

interface final members

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more doubt from Dan's mock exam
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();
}
}

What is the result of attempting to compile and run the above program?
a. Prints: ABCI
b. Runtime Exception
c. Compiler Error
d. None of the Above
..

acording to me the ans shud be complier err since a constant declared in a interface is final so cannot be changed once implemented by a class .Am i right ... but the ans is given as a
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vidya
Like if you extend a class and you inherit a variable from that class you can then, also, declare a same name variable. Similarly this happens for interfaces too. The new variable now shawdows the older one but now if you wish to use the older inherited variable you will have to use the appropiate syntax like in the question.
[ May 30, 2003: Message edited by: Anupam Sinha ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As interface fields are implicitly public and static, you can replace this code:
((I)this).s1
with this:
I.s1
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would be illegal: I.s1 = "new value";
When I see questions such as this, I wonder why the person doesn't just compile it to see what happens.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic