File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Innner Class

 
Supree
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Can inner class be abstract?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you compile the following?
<PRE>public class Test {
abstract class x{}
}</PRE>
 
Supree
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony.
Got you.
Innner class can be abstract
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
If the inner class is an abstract one ,is it required that outer class shoud be declared as an abstract class. cos the non-static inner class is some thing like a class instance variable or method right?? ( ofcourse i got the answer that it need be!! am looking for any explanation !! thanks in advance)
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my example above. I never post code like that without compiling it first. So if an inner class is abstract, the outer class doesn't have to be.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Oh Sorry there is typo there that's what i mean it " need not be".My question is why is it so ? . Normally if any one method of a class is abstract then class must be defined as abstract isn't it?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - but that's just the relationship between methods and the class that contains them. There is no such rule for inner classes and the class that contains them.
The reason that abstract methods force the entire class to be abstract is to prevent the possibility that a class could be instantiated without an implementation of one of its methods. In the case of abstract inner classes, it is already impossible to instantiate the inner class (as soon as you call a constructor for it, you get "can't instantiate abstract class"). So there is no way to write any code that would successfully access any abstract (undefined) features, and additional "protection" (in the form of an additional rule) is unnecessary.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic