jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is a class subclass of itself? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is a class subclass of itself?" Watch "Is a class subclass of itself?" New topic

Is a class subclass of itself?

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I remember that I have seen a Question in some Discussion (I don't remember where )asking 'Is a class subclass of itself?'. In order to verify this I wrote this prog and the compiler complains as it is cyclic inheritance. To confirm that again I removed that extends part and instead of ' new Howru()I wrote new Object(). It gives NullPointerException at runtime which confirms that NO SUPER CLASS for java.lang.Object which is what the JDK also says as public class Object. But in R&H Book in API reference pg 787 it says as public class java.lang.Object extends java.lang.Object. I checked in their error pages also . It doesn't say anything about this. For sure we know that there is NO SUPERCLASS for java.lang.Object. When I saw this line in their page like this, I asked myself ( before I coded and checked this prog) , 'Is this means a class is subclass of itself?'. So moral of the story is do not take for granted when you read. Verify it by coding and testing. SO I think the answer for the above question is 'NO'. What do you all say ?

[This message has been edited by maha anna (edited February 17, 2000).]
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
Bertrand Meyer in his book OBJECT ORIENTED SOFTWARE CONSTRUCTION, 2nd ed. defines a "descendant" of a class to be its subclasses and itself too. Then he defines "proper descendant" as all the previous ones minus the base class.

The important thing is that you learnt that no, "you can't believe everything you see or hear"

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
It's all a matter of how you chose to define "subclass". I think Meyer's definition is not what's commonly used, but at least he defines it clearly. The JLS at least seems to use a different definition, e.g. "A class may be declared final, in
which case it cannot have subclasses." This statement makes no sense if a class is a subclass of itself. So I'd agree the best answer is "no", but beware that not everyone may understand "subclass" to mean the same thing.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: Is a class subclass of itself?
jQuery in Action, 3rd edition