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

Inner Class Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am currently reading the Sybex Roberts book "The Complete Java 2 Cert book..yadda yadda yadda....there is a statement that I was wondering if someone could clarify for me. On page 192 it says that Anonymous inner classes may implement interfaces or extend other classes. Now I understand how the implicit implementation for interfaces takes effect, however, I was wondering about the "extend other classes" statement. Does this mean I should be able to create a new class anonymously like
new AnonymousClass() extends SomeClass {...}
or does this mean that you can perform an anonymous class instantiator for any class which extends another...Basically all the classes.
Any help would be appreciated.
RR
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
There are two important points regarding Anonymous classes.
1. Anonymous inner classes may implement an interface
The syntax for this is:

new InterfaceName(){
// body

}
where, we should assume that there are hidden words between the "new" keyword and "InterfaceName" which are "class" "implements"
2. Anonymous inner classe may extend another class
To extend an existing class, the arguments for the superclass constructor may be placed in the argument part of the new expression,
new SuperClass(Message){
//body
}
Here again, "class" "extends" is hidden.
This is what I have understood so far about the anonymous classes. I may be wrong. So, please feel free to correct me.
More info can be found in RHE
Regards,
Preethi :-))
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you may as well use inner classes to extend classes. However, the syntax in your post is not correct. You have to use the same syntax as for interface implementation.

The correct syntax is either:


or

Let's take an exemple:

Suppose you are designing an User Interface in which a user may be able to select files. For this purpose you would use a FileChooser (javax.swing.FileChooser). However, you do not want your see all directory or files named "_private", since they contain your application configuration information. Java offers a special class called FileFilter (javax.swing.filechooser.FileFilter) which is used to filter the files listed in the "Open" file dialog.

You could write code similar to this:


Hope this helps!
Regards,
Beno�t
[This message has been edited by Beno�t d'Oncieu (edited October 26, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'rrutan',
Welcome to JavaRanch.
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
I am going to test your electrical conductivity with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic