This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding Innerclass.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarding Innerclass...." Watch "Question regarding Innerclass...." New topic
Author

Question regarding Innerclass....

Justin Smith
Greenhorn

Joined: Jul 24, 2008
Posts: 19
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

Joined: Jul 11, 2006
Posts: 143
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.


Studying for SCJP 6
Justin Smith
Greenhorn

Joined: Jul 24, 2008
Posts: 19
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

Joined: Jul 24, 2008
Posts: 19
One more question...

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

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
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.
 
 
subject: Question regarding Innerclass....
 
Similar Threads
Can we declare main() method in non-static inner class
Inner Class
Inner classes within inner classes
"inner class"
Have a question about inner class