Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Hemant Khandal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of one way of subclassing the inner abstract class and access that method.

 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe i just can't see outside the box, but why would you need an abstract inner class?
 
Paul Clapham
Sheriff
Posts: 21305
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21305
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic