aspose file tools*
The moose likes Java in General and the fly likes How can acces inner class content from outer class if inner class is abstract? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can acces inner class content from outer class if inner class is abstract?" Watch "How can acces inner class content from outer class if inner class is abstract?" New topic
Author

How can acces inner class content from outer class if inner class is abstract?

Hemant Khandal
Greenhorn

Joined: May 29, 2011
Posts: 11
public class Outer {
abstract class MyInner{
public void seeOuter(){
System.out.println("Inner class Ref is:" + this);
System.out.println("Outer class Ref is:" +Outer.this);
}
}
}

Now, how can we access seeOuter() from outer class?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I can think of one way of subclassing the inner abstract class and access that method.

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

maybe i just can't see outside the box, but why would you need an abstract inner class?


SCJP
Visit my download page
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Randall Twede wrote:why would you need an abstract inner class?


You would need an abstract inner class because you need a class which is abstract and which is an inner class. You don't need to look outside any boxes to figure that out. Inner classes have certain features which are useful. Abstract classes have certain features which are useful. It's possible you might need both of those features at the same time.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Paul Clapham wrote:
Randall Twede wrote:why would you need an abstract inner class?


You would need an abstract inner class because you need a class which is abstract and which is an inner class. You don't need to look outside any boxes to figure that out. Inner classes have certain features which are useful. Abstract classes have certain features which are useful. It's possible you might need both of those features at the same time.


Um, all you've done is to say that you would need it when you would need it, and that it might be useful. Can you (or anybody) suggest a situation in which this would be the case? The inner classes I have written are either implementations, such as listeners, label providers etc, or helper classes particular to the enclosing class and of no utility or interest outside of that class. In other words, my experience with inner classes is that specificity is part of their essence. Can someone help me to understand how an inner class could ever be useful as an abstraction? Conversely, my experience with abstract classes is that they provide general utility. Can someone help me to understand how being an inner class is congruous with that role?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

One important feature of an inner class, as you know, is that it has access to the members of the class which contains it. So it might be that you write an outer class which has some particular purpose, and it exposes an inner class which has some related purpose. I'm thinking of Map and Map.Entry as an example of that. Actually there you have an example of an inner interface, which is clearly intended to be subclassed via implementation. From there it isn't too much of a stretch to imagine an inner class which is also intended to be subclassed. Naturally this could be an abstract class. (I also haven't ever created such a class, since my programming is also mostly for specific purposes.)

It's also true that Java allows the programmer to do things which are useless or even stupid by combining features. For examples of useless and stupid things allowed by Java see pretty much most of the questions in the forums about certification exams here.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Dennis put it more succinctly than i did. i just cant see a need for an abstract inner class. unless you have dozens of concrete inner classes that inherit? that doesn't sound good. c'mon this is a dare you to prove me wrong thing

lol Paul...what's up with all the foo bar stuff anyway?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Paul Clapham wrote:I'm thinking of Map and Map.Entry as an example of that. Actually there you have an example of an inner interface, which is clearly intended to be subclassed via implementation.

Ok, that works. Thanks.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I actually did use an abstract inner class recently. I've got a rather complicated DAO class that loads series of data from the database. I used inner classes to construct series of the data loaded from recordset, and I needed several of these classes to handle ints, doubles and Strings (numbers are stored in arrays of primitives; this is why I couldn't use generics for that). I've put common logic of these classes into an abstract inner class and only overrode the abstract method that read the data from the recordset and put it into the proper primitive array.

It all came as quite natural to me. I had three children of the abstract class and I would do it the same even if I had only two. Creating a common ancestor only when the number of related classes reaches a dozen or so is quite clearly not a good practice.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can acces inner class content from outer class if inner class is abstract?