Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt regarding Inner Class

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we can extend from any class except the class marked final,I want to extend from an inner class.

My program is like this

class outer
{

class inner
{

public void show()
{

System.out.println("show in inner");
}

}//End of Inner

}//End of Outer


class innerDemo extends outer.inner //Line A
{

public static void main(String ar[])
{
}

}

Is Line A a valid one?
When i compile this Iam getting the error as

An enclosing class instance that contains outer.inner is required.



What does it mean?


One more thing is as we can apply the modifier abstract to an inner class,how to write a concrete subclass which implements this abstract inner class?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of an inner class can exist only within an instance of the outer class.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code.. This will compile
class outer
{
class inner
{
public void show()
{
System.out.println("show in inner");
}
}//End of Inner

public class innerDemo extends outer //Line A
{
}

}//End of Outer
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An inner class can be extended by another class. But it would need an instance of itsenclosing class. Think of the inner class as a non-static member of a class. Now whenever you want to use a non-static class member, you need to have an object of its enclosing class to use it. The case is no different for inner classes as well. You always need an instance of its enclosing class to use it.

The code below will make things clearer:



O/P:
Outer is being instantiated
Inner is being instantiated
Other is being instantiated

[ January 28, 2008: Message edited by: Satya Maheshwari ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I tested the code it works perfect. But could you explain for me about:



Why use ref.super() ?
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Arjun Cheng:
Hi, I tested the code it works perfect. But could you explain for me about:


Why use ref.super() ?


Hi Arjun,
That is a good question. I tried to dig in the java specs(8.8.7.1) and found as below:

[B]Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass. Superclass constructor invocations may be further subdivided:
Unqualified superclass constructor invocations begin with the keyword super (possibly prefaced with explicit type arguments).
Qualified superclass constructor invocations begin with a Primary expression . They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (�8.1.3). This may be necessary when the superclass is an inner class.
Here is an example of a qualified superclass constructor invocation:

[/B]



Hope this clarifies your doubt.

Regards
SatyaDeep
[ January 30, 2008: Message edited by: Satya Maheshwari ]
 
Arjun Cheng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you help. It's very hlepful.
 
Your buns are mine! But you can have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic