• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Abstract class..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no response?...waiting for reply..
 
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}
}
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question!!

This works as well:

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

Thanks, Pat
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you both
now i got two ways to solve this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic