• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding Innerclass....

 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fellowranchers....

I have a doubt regarding regular inner class(non-static,non-anonyms). Can we create a regular Inner class inside a abstract class??? If so what is the use of it???
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I tried it out, but I won't share my results. I'll leave that up to you to try out. I used a public inner class inside an abstract default class. Then in a public class, I tried to instantiate the inner object. This is done by a statement like this:

Then I extended the abstract class with a default level class, and tried to instantiate the inner class from there:

At least tell us what you think might happen... We're more than happy to help you further.
 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

This is what i did....


-------------
abstract class InnerTest
{
protected InnerTest(){}
private int x =6;
public static void main(String[] gea)
{
System.out.println("Just here to say sorry i cannot instantiate InnerTest"); // won't get printed

}
abstract void myPieceofCake(String cake);
void howToGetToInner(String takeInner)
{
System.out.println("Am I Inside howToGetToInner(string) method???");
InnerMost im = new InnerMost();
im.seeOuter();
}
class InnerMost implements java.io.Serializable
{
public void seeOuter(){ System.out.println("The value of 'x'is:"+x); }
}

}

public class TestAbsInner extends InnerTest
{
void myPieceofCake(String ake)
{
System.out.println("Ofcourse it is my piece of cake");
}
void toTakeInner(String test)
{
System.out.println("To get the innermethod of an abstract class");
howToGetToInner(test);

}
public static void main(String[] gea)
{
TestAbsInner tai = new TestAbsInner();
String test= "JavaRanch";
tai.myPieceofCake(test);
tai.toTakeInner(test);
}
}
-----------------

And the output which I got is this:

Ofcourse it is my piece of cake
To get the innermethod of an abstract class
Am I Inside howToGetToInner(string) method???
The value of 'x'is:6

 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question...

Can I create an Inner of Inner??? What will happen if I create one???

 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So yea, it's possible to do only if you extend the abstract class. Without even trying it first, I think you can nest classes as much as you want. It might be a bad idea... Yep, it works just fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic