Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extending inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extending inner class" Watch "Extending inner class" New topic
Author

Extending inner class

av bharathiraja
Greenhorn

Joined: Aug 24, 2006
Posts: 7
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.


Regards<br />AV
Nandini Bhaduri
Greenhorn

Joined: Oct 23, 2006
Posts: 11
Yes, it is possible.
av bharathiraja
Greenhorn

Joined: Aug 24, 2006
Posts: 7
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 ]
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
Andrea Z�rcher
Greenhorn

Joined: Oct 06, 2006
Posts: 10
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.


SCJP 5.0
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
All of them can be subclassed except for anonymous classes.


"I'm not back." - Bill Harding, Twister
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Jim,
Can you give an example of each of them.I tried but couldnt make it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
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


SCJP 1.4, SCWCD 1.5
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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

Joined: Oct 03, 2006
Posts: 125
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending inner class
 
Similar Threads
Passing an object to a method.
Private
Inner Classes
Question in K&B Book - protected class
Inner Classes