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???
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.
------------- 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
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 by:autobot
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