• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Subclass

 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Vivian Josh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic