• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding inner class methods?  RSS feed

 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
if I have the following code:

is it possible to:
1. override the actionPerformed method in class B?
2. invoke the code in the actionPerformed method in class B (using super)?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. override the actionPerformed method in class B?
Sure:

2. invoke the code in the actionPerformed method in class B (using super)?
Not sure what you are asking here. Do you mean the super of class B, which is A, or the super of the extended inner class IA?
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
Thanks for your answer to question 1.
As for 2, I want to call the actionPerformed method in IA, but
super.actionedPerformed();
doesn't look right!
best regards
Simon
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The A class as you have it written would not compile. You can't have code (the call to addActionListener) just sitting in a class without being within the scope of a method.
This leaves the question ambiguous. Are you declaring a top-level inner class, or are you declaring the IA class within a method? Both are legal:

In the example above, there is an inner class Bar, and there is an inner class Baz that is local to the bash() method. Which is IA in your example?
Referring to my example, note that the inner class Bar would not be accessible to a subclass because it is not declared public or protected--it's package-private, so like any other class-level variable or method, it's not accessible from a subclass.
sev
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.actionedPerformed();
Well, you're a lot more Java savvy than you realize, 'cause that's it. It's like when you spell a word and it just doesn't look right, but it is.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The A class as you have it written would not compile. You can't have code (the call to addActionListener) just sitting in a class without being within the scope of a method.
Actully that's not quite true, it is true that Simon's class would not compile but you can place the call to addActionListener in an instance block like I did in my example. But that may be splitting hairs since all instance blocks are copied into all constructors verbatim.
This leaves the question ambiguous. Are you declaring a top-level inner class, or are you declaring the IA class within a method? Both are legal:
It seems clear to me that IA is an instance (inner) class of A.
Referring to my example, note that the inner class Bar would not be accessible to a subclass because it is not declared public or protected--it's package-private, so like any other class-level variable or method, it's not accessible from a subclass.
Not true if the two classes are in the same package which they would be if defined in the same file.
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
Thanks very much. It's all working fine now.
regards
Simon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!