Author Topic:   Is a class subclass of itself?
maha anna
bartender
posted February 17, 2000 09:53 AM             
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 ?

code:

class Howru { //'class Howru extends Howru' gives cyclic inheritance error
public static void main(String[] args) {
Object obj = new Howru();
Class objClass = obj.getClass();
Class superClass = objClass.getSuperclass();

String superClassName = superClass.getName();
String objName= objClass.getName();


//
System.out.println("SuperClassName="+superClassName);
System.out.println("getNameStr="+objName);

}

}


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

Tony Alicea
sheriff
posted February 17, 2000 10:03 AM             
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"

Jim Yingst
sheriff
posted February 17, 2000 12:44 PM             
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.

|