Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Extending inner class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to create a class that extends an inner class of another class. also kindly let me know the links/copies of mock exam.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is possible.
 
av bharathiraja
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried by extending a class with subclass residing at another class
like myclass extends myOuter.myInner{
public static void main(String a[])
{ }
}

class myOuter{

class myInner{

}
}
but it was throwing Class can'nt be resolved error at compile time.

Could you give me an example
[ December 22, 2006: Message edited by: av bharathiraja ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The inner class can not reached with out a outer class so inner class can not be inherited,If I am not wrong.However a static inner class can be inherited like

It is invalid to inherit the Inner class by its enclosing outer class.

The above code is invalid.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should differentiate between the following types of inner classes:

- (regular) inner class
- method-local inner classes
- anonymous inner classes
- static nested classes

If I�m not wrong, only static nested classes can be subclassed.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All of them can be subclassed except for anonymous classes.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
Can you give an example of each of them.I tried but couldnt make it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by av bharathiraja:
... Could you give me an example


 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
The inner class can not reached with out a outer class so inner class can not be inherited,If I am not wrong.However a static inner class can be inherited like

It is invalid to inherit the Inner class by its enclosing outer class.

The above code is invalid.




hi Sanjeev,
you are in big confusion as you said ,"inner class can not be inherited". we can extend Inner classes as well.please try the code below.

class Outer{
class Inner{
}
}
class OuterDemo{
class InnerDemo ectends Outer.Inner{
public InnerDemo(){
Outer.super();
}
}
}

as InnerDemo class does not no which object to bind to so the non-default
constructor is necessary to intimate Outer class that it has an outer
object to bind to.
However this outer object problem is not arised if OuterDemo ectends Outer
class.in that case we need not use non-default constructor.

hope this will help .
if still confused please let me know.

Regards.
MAneesh Saxena
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Raunak for pointing out my errors.The thing I was trying is to extend a inner class by a top level class,which seems to be impossible.All the class which are extending an inner class are itself inner.
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
Thanks Raunak for pointing out my errors.The thing I was trying is to extend a inner class by a top level class,which seems to be impossible.All the class which are extending an inner class are itself inner.



hi again,

dear sanjeev we can also extend Inner classes by top level class.the thing is same again it requires the same non-default constructor to intimate Outer class that it has an outer object to bind to.

class Outer{
class Inner{
}
}
class OuterDemo extends Outer.Inner{
public OuterDEmo(Outer ob){
ob.super();
}
}

try above syntax it'll surely remove your confusion.

Regards
Maneesh Saxena
 
reply
    Bookmark Topic Watch Topic
  • New Topic