• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

abstract inner class??

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Exam Cram says :
Inner class - member : A named class defined as a member of the enclosing class must be associated with an instance of the enclosing class.There can be multiple member inner classes in an enclosing class.Member inner classes can be declared as public, private, protected,final, or abstract,but cannot have the same name as the enclosing class.
Could somebody tell me how to call an abstract method of an abstract inner class from its enclosing class ? Or , how do we use an abstract inner class??
please help me with the following code:
class AbstractOuter
{
private int i = 10;
abstract class AbstractInner
{
abstract void amethod();
}
abstract class newAby extends AbstractInner
{
void amethod()
{
System.out.println("from the abstract inner subclass");

}
}
void makeInner()
{
AbstractInner ai = new AbstractInner();//abstract class
//AbstractInner
//cant be instantiated
ai.amethod();
}

public static void main(String [] args)
{
AbstractOuter ao = new AbstractOuter();
ao.makeInner();
}
}

thanks ,
Vidya.
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oo that was dumb...
this is the correct code..
I got it right....

class AbstractOuter
{
private int i = 10;
abstract class AbstractInner
{
abstract void amethod();
}

class newAby extends AbstractInner
{
void amethod()
{
System.out.println("this comes from the abstract inner subclass");
}
}
void makeInner()
{
newAby aa = new newAby();
aa.amethod() ;
}

public static void main(String [] args)
{
AbstractOuter ao = new AbstractOuter();
ao.makeInner();
}
}
Thanx anyway...
Vidya.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled the class & was expecting error as the member class was abstract. However, it got compiled?
So, only if member methods as abstract, then only you need to declare top class as abstract?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
"Could somebody tell me how to call an abstract method of an abstract inner class from its enclosing class ? Or , how do we use an abstract inner class??"
I thought abstract class cannot be instantiated thus cannot "use" its methods.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! The name of the outer class "AbstractOuter" is kinda misleading. The outer class is not abstract. Only the inner class is abstract. It has an abstract method which is implemented
in the subclass "newAby"(which is legal becus Abstract classes cannot be instantiated but can be subclassed). The outer class has a method "makeInner" which creates an instance of the subclass of the inner class( legal because "newAby" is not abstract). Then using this instance the "amethod()" is invoked.
When an instance of the outer class was created in main() and the "makeInner()" invoked, it in turn created the "newAby" instance and invoked the "amethod()", thus printing out the message
"this comes from the abstract inner subclass".
Hope this helps.....:-))
I may have missed out something or there may be an error, so please feel free to point it out.
Preethi
 
reply
    Bookmark Topic Watch Topic
  • New Topic