aspose file tools*
The moose likes Beginning Java and the fly likes Nested Subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Subclass" Watch "Nested Subclass" New topic
Author

Nested Subclass

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Hi Ranchers,

Can someone explain me what is a nested subclass with example ?

I know nested top-level class but don't know about nested subclass.


Regards,
Vivian
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

I know nested top-level class but don't know about nested subclass.

"Nested top-level class", that's a contradiction. A class is either a top-level class or a nested class, but cannot be both at the same time.

Nested classes are classes inside other classes. See: The Java Tutorial - Nested Classes. Inheritance and subclassing is a different concept, and you can combine nesting and subclassing.

Here's an example of a nested class that's a subclass of its enclosing class:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

The java.awt.geom package contains several classes with nested subclasses. For example, Point2D has nested subclasses Point2D.Double and Point2D.Float.

This has one peculiar property: the nested subclasses inherit themselves! For instance, the following code is 100% valid:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jesper]: "Nested top-level class", that's a contradiction. A class is either a top-level class or a nested class, but cannot be both at the same time.

To be fair, when Sun first introduced nested classes to Java, they defined things a bit differently, and "top-level nested class" was the official Sun-sanctioned term for what we now call a static nested class (or static member class). This was a horribly nonsensical choice of terminology, and in 2000 the Java Language Specification 2nd Edition came out and removed this particular term. Not everyone realizes this though, and some people, sadly, continue to refer to top-level nested classes. Of course, there are also people who refer to "static inner classes", but those people were always wrong. Either way, the correct term now is static nested class, or static member class.


"I'm not back." - Bill Harding, Twister
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Hi All,

Thanks for the reply an explanations .

Sun still refers static nested classes as top-level inner classes. My latest SCJP book (KS & BB,1.5) has this term.

But there are these nested subclasses . I have seen the same example of Point2D on internet but could not understand properly.

Rob , can you explain how exactly it differers from regular nested classes with simple example ? Or can you send me some link where it is explained in detail?

Thanks for the help,
Vivian
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Vivian]: Sun still refers static nested classes as top-level inner classes. My latest SCJP book (KS & BB,1.5) has this term.

OK, but KS & BB are not Sun. The JLS now has fairly clear, straightforward definitions of these words: "A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class." There is no overlap between the two. (Anymore.)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Originally posted by Vivian Josh:
Rob , can you explain how exactly it differers from regular nested classes with simple example ? Or can you send me some link where it is explained in detail?

The example I showed you should never be used

It was just an example to show you that the nested subclass inherits EVERYTHING from the main class. This also includes the nested subclass itself.

Consider basic inheritance:

Class B inherits the constant, inner class and public field from class A.

Now consider the case where B is a nested class of class A:

Now not only does class B inherit the constant, inner class InnerA and the public field, but it also inherits itself! Therefore, it would be possible to call class B through class B itself:

Altough in practice you would never use the second or third possibilities, it is still valid code.
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Thanks Rob !!

That was really nice & simple example.

But wont it be recursive thing? Why would someone want to do such thing? Any peculiar reason or condition where one wants to implement such code?


- Vivian
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There is no reason to do this, ever. The fact that it's possible at all is just because of an accidental overlap between two rules:

1. a nested class is a members of its enclosing class

2. a subclass inherits members from its superclasses

If the enclosing class is the superclass, then the class inherits itself as a member. Which is useless, but also harmless. Sun could have put in a special rule preventing this from being possible, but then people would ask "why isn't this possible?". It's kind of a waste of time either way.

The code above isn't recursive - there's no possibility of an infinite loop here; it's entirely the programmer's decision whether to type A.B or A.B.B or A.B.B.B, etc. The process stops automatically as soon as the programmer decides to stop wasting time typing in meaningless code.
[ October 31, 2007: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Subclass