• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Use of this and super in interfaces

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
int i;
}
interface C {
Test t = new Test() {
int i = super.i;
};

}
The above code does not compile with JDK1.2.2. But it does with jikes.
Does it compile with jdk1.3 ?
Is there anything wrong with the code ?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
You can use "super" only in a derived class. So if you say "class C extends Test" then you can use super.i in "C". For argument sake you CAN use super in any class since all are implicitely derived from Object but then you can only call methods of the Object class.
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Uvnik Gupta:
Deepak,
You can use "super" only in a derived class. .


I AM using super in the derived class of Test.
Test t = new Test() {
int i = super.i;
};
Here, the anonymous class in the initilizer of the constant field 't' has Test as the super class.
Therefore, super is allowed here. Hence it DOES compile with jikes.
Refer JLS 9.3.1:
If the keyword this (�15.8.3) or the keyword super (15.11.2, 15.12) occurs in an initialization expression for a field of an interface, then unless the occurrence is within the body of an anonymous class (�15.9.5), a compile-time error occurs.


 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong with the code, JDK 1.3 compiles it OK.
 
reply
    Bookmark Topic Watch Topic
  • New Topic