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

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

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

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe i just can't see outside the box, but why would you need an abstract inner class?
 
Marshal
Posts: 28304
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28304
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like 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