• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing a nested toplevel interface

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi everybody,
Suppose I want to implement a nested top level interface called "Transaction" which is inside a class called "Bank". Can I do it by the following two ways :

1) public class Test implements Bank.Transaction

2) import Bank.*;
public class Test implements Transaction
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
Where is the interface Transaction defined? Which one of the two approches work depends on where the interface is and what are the access restrictions that govern its visibility.
Infact there is little difference between your two approaches. The fully qualified reference Bank.Transaction is redundant unless you have another interface Transaction in the immediate scope that shadows the "original" interface Transaction. Using the dot (.) to resolve the ambiguity you are telling the compiler to resolve the reference using the "Bank" namespace.
The second approach works fine( and importing is required ) if the interface Transaction is defined in a different package( in your case the package is called Bank ) and
it is not visible to the Test class.
Ajith
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Ajith,
How can I have two nested top-level interfaces in the same class?
What is the difference if the class Bank in which the interface Transaction is nested is in the same or different package as my class or not?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this example. It is self explanatory.

Hope that helps,
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I forgot about the second part of your question.
The visibility of interfaces across different packages is governed by the same rules that hold good for classes. For nested interfaces( interfaces defined within in classes, like in my example above, ) the enclosing class itself should be visible to the class that implements the interface. This means you should first import the package that defines the enclosing class and then use the dot resolution to reference the nested interface in the implements clause.
Ajith
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic