Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class..

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
no response?...waiting for reply..
 
Shwetha Sharma
Ranch Hand
Posts: 34
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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();

}
}
 
patrick avery
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to you both
now i got two ways to solve this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic