• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt on Inner class  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 59140
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch the UPPERCASE, please.

Inner classes are far too complicated for the beginner's forum . . . moving.
 
Let's go to the waterfront with this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!