File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract class.." Watch "Abstract class.." New topic
Author

Abstract class..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi,
Can we declare an inner class in abstract class?. i tried this and it complies,but i couldnt able to access that class.any way to access inner class? and what will be the use of doing so?
Here is the program which i tried it on my own...

abstract class AA
{
abstract void method();
class BB extends AA{
BB()
{
System.out.println("BB constr");
}
public void method()
{
System.out.println("method of BB");
}
}
}



class CC extends AA
{
CC()
{
System.out.println("CC contr");
}
public void method()
{
System.out.println("method of CC");
}

public static void main(String args[])
{
CC c=new CC();
c.method();
//what can be added here to access innerclas
}
}

Please correct me if this is senseless...

Thanks in advance
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
no response?...waiting for reply..
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

Hi Preetha,
We can do that by creating an instance anonymous subclass of the abstract class which could be used to access the inner class

abstract class AA
{
abstract void method();
class BB extends AA{
BB()
{
System.out.println("BB constr");
}
public void method()
{
System.out.println("method of BB");
}
}
}



class CC extends AA
{
CC()
{
System.out.println("CC contr");
}
public void method()
{
System.out.println("method of CC");
}

public static void main(String args[])
{
CC c=new CC();
c.method();
//Code added to access innerclass
AA aa = new AA(){void method(){}}; //Here aa is the instance of anonymous subclass of Abstract Class AA
AA.BB bb = aa.new BB(); //bb is an instance of the inner class
bb.method();

}
}


SCJP6:80%, OCWCD 88 %
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
Good question!!

This works as well:

CC.BB b = c.new BB();
b.method();

Thanks, Pat


SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Thanks to you both
now i got two ways to solve this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class..