• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on Inner class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a simple code which uses "Inner class " concept for specific inheritance.
I want to Inherit in Class C the Class B method(demo method) which is a Inner class of A

When I am tyring to compile the error was " AN ENCLOSING INSTANCE THAT CONTAINS A.B IS REQUIRED".
could someone explain what that means.
Thank you.

[edit]Add code tags CR[/edit]
[ December 14, 2008: Message edited by: Campbell Ritchie ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The B class is an non-static inner class of Class A. It must have an enclosing instance of A in order to exist. Consider it like a variable: If B were a non-static variable in a you would need to have an instance of A in order to access it. The same goes with non-static inner classes - they can't exist without the outer class.

So when you try to extend the inner class B from C, you are trying to make it possible to create an instance of B without a surrounding instance of A, which is impossible.

The inner class B actually has access to the enclosing-classes members, so if A had a private method or member variable B may be using it. Without an enclosing instance of A, how would class C use A's member methods and variables?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot extend B this way unless B is static or C is also an inner class of A.
As you have a constructor for A, which means you need to instantiate A to access non-static fields of A but again you cannot use instances to define a class. That says ,even if you create C as an inner class of some other class D and then try to define C as "C extends a.B" where a is an instance of A then it will say "a" cannot be resolved as a type (where "type" is a keyword, and an instance is not a type)

If you still want to inherit B and not A you will have to do it this way
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed Steve..
i was formulating the reply when your post came up
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Just to warn you, I am a beginner in Java, so don't believe a word I say. I thought this might help though:

In your original code, you tried to call the demo method by using



But, demo() is the inner class B's method!

Although class C does inherit an inner class following the blueprint from class A, you still need to create an instance of class B ( a 'B' object ). That object then gets created and that object can use IT'S demo() method as I've shown below.



Hope this helps :-)

Cheers

Joe
[ December 14, 2008: Message edited by: Joe Lemmer ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Watch the UPPERCASE, please.

Inner classes are far too complicated for the beginner's forum . . . moving.
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic