Forums Register Login

Question regarding Innerclass....

+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
One more question...

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

+Pie Number of slices to send: Send
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.
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 767 times.
Similar Threads
"inner class"
Can we declare main() method in non-static inner class
Inner Class
Have a question about inner class
Inner classes within inner classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:17:05.