I don't think I would say your reason is correct, as stated. It really
isn't a substitute for extending classes, directly.
However, I would like to highlight a key point in what you're saying
-- not only is it not possible to extend multiple classes, it's not a
good idea to throw a bunch of unrelated functionality into one class.
The big advantage of inner classes is that they can reference fields
of the outer class, while providing a bit of focused functionality.
For example, you can have a callback, whose single job is to update a
view with new data, when it's available from a server. It would have
access to the server, and to the view, and only those items. This
clear responsibility makes it much easier to understand, than a method
on a single object that implements many different interfaces, and
tries to be a service, a view, a database owner, an application, a
game board, and 16 different callbacks!
The other thing to realize is that ANONYMOUS inner classes can
reference final variables within a method. These effectively become
final fields of the anonymous inner class -- but without your having
to make them such. So, for example, you can have a function that
iterates over a list of buttons and a list of images, and with a
single inner callback class, link the buttons to display the images in
a view.
// Completely made-up example, not Android code!
class MyClass {
...
private void setupButtons(final View container, List<Button>
buttons, List<Image> images) {
for (int i = 0; i < Math.max(buttons.size(), images.size()); i++)
{
Button button = buttons.get(i);
final Image image = images.get(i);
button.addOnPressed(new OnPressedHandler() {
public void onPressed() {
container.setContent(image);
}
});
}
}
}
I deliberately made the API different than from Android to force you
to think about this abstractly. Let's compare this with what you'd
have to write without inner classes:
class MyButtonHandler implements OnPressedHandler {
private final Image image;
private final View contaner;
public MyButtonHandler(Image image, View container) {
this.image = image;
this.container = container;
}
public void onPressed() {
container.setContent(image);
}
}
class MyClass {
...
private void setupButtons(final View container, List<Button>
buttons, List<Image> images) {
for (int i = 0; i < Math.max(buttons.size(), images.size()); i++)
{
Button button = buttons.get(i);
final Image image = images.get(i);
button.addOnPressed(new MyButtonHandler(container, image));
}
}
}
Not only is this a bit more code, and a bit more work to set up -- it
moves the logic of what the button does to a different location --
likely a different file. This makes it harder to read and understand
the resulting code, when the code in the handler is simple, as in this
case. Looking just at setupButtons -- what happens when you press the
button? You can't answer that without finding MyButtonHandler, and
reading it. This locality is a big reason for inner classes, and
anonymous inner classes, on top of the convenience of sharing state.
You'll notice I keep using callbacks as an example. That's not the
only use, but it is arguably the most important, and callbacks in UI
code were a major driver to adding them to the language, long ago
(almost 14 years ago now).
I'll also point out that local classes that are NOT anonymous are also
possible, and sometimes a good idea, but seldom actually seen. That
will look like this:
class MyClass {
...
private void setupButtons(final View container, List<Button>
buttons, List<Image> images) {
for (int i = 0; i < Math.max(buttons.size(), images.size()); i++)
{
Button button = buttons.get(i);
final Image image = images.get(i);
class MyOnPressedHandler {
public void onPressed() {
container.setContent(image);
}
}
button.addOnPressed(new MyOnPressedHandler());
}
}
}
That can be handy or even essential if you are using the same local
class in multiple places.
On Oct 26, 7:27 pm, zeeshan mirza <[email protected]>
wrote:
> Frank thank you for your answer. I know some points when do we need to
> create inner classes like we cant extend more than one class so if we
> create inner classes we can extend more classes. Am i right? is there
> any other advantage of inner classes ?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en