aspose file tools*
The moose likes Java in General and the fly likes Doubt on abstract class with Inner class concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt on abstract class with Inner class concept" Watch "Doubt on abstract class with Inner class concept" New topic
Author

Doubt on abstract class with Inner class concept

Martin Arun Paul Perumala
Greenhorn

Joined: Dec 10, 2008
Posts: 14
Below is a simple code using Inner class concept.(outer class is abstract and inner class is concrete class).
I want to inherit class B(inner class) only in class C(specific inheritance)

when I am trying to compile the program the error is "an enclosing instance that contains A.B is required." which means to instantiate the class A ?
But class A is abstract which cannot be instantiated.
please help me


[edit]Add code tags. CR[/edit]
[ December 18, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You can instantiate an abstract class as an anonymous class. Can't remember how to do it, but try new C(){};
Please use the CODE button to maintain indentation; it makes the code easier to read.
And inner classes are hardly a beginner's topic: moving.
Martin Arun Paul Perumala
Greenhorn

Joined: Dec 10, 2008
Posts: 14
could you mind if you please give some examples on using codes in the above program.I will try to use them in the next post.
Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Do you mean the CODE button? You click the button below the "message" window, then {CODE} {/CODE} appears, only the {} are really [].

Then you write your code (or better use ctrl-c ctrl-v) in the gap between ] and [. Then it maintains indentation.

You can also write the code, highlight it, and push the CODE button; it has more-or-less the same effect. You can see how much better the code looks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Use Code Tags says it all


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

I believe this is a re-post of Your other Thread from a few days ago.

Like I said in the other thread, an Object of the inner class B can only exist in the context of an instance of the outer class A. By extending B in a class that does not provide an instance of A as a context then you will be breaking B's ability to refer to its outer class instance. You can't do that.

Also as noted in the other thread you could get around it by extending the Abstract outer class first, then extend the inner class inside the now concrete version of the outer class:


You can also cheat a bit and implement an anonymous extension of the outer class to hold the inner class:


p.s. You should read This Campfire Story to learn more about inner classes.
[ December 18, 2008: Message edited by: Steve Luke ]

Steve
Martin Arun Paul Perumala
Greenhorn

Joined: Dec 10, 2008
Posts: 14
Thanks a lot ..Now I understand
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on abstract class with Inner class concept