Java: Is a class a subclass of itself?
https://stackoverflow.com/questions/839569
OTHER TIPS
The definition of subclass is that it extends another class and inherits the state and behaviors from that class. A class cannot extend itself since it IS itself, so it is not a subclass. Inner classes are allowed to extend the outer class because those are two different classes.
public class ParentClass {
int intField = 10;
class InnerClass extends ParentClass {
}
public static void main(String... args) {
ParentClass parentClass = new ParentClass();
InnerClass innerClass = parentClass.new InnerClass();
System.out.println(innerClass.intField);
InnerClass innerClass2 = innerClass.new InnerClass();
}
}
Actually JVM doesn't now anything about inner classes.
All inner classes become usual classes after compilation but compiler gives them accessors to all fields of outer class.
In OOP theory, class cannot be its subclass or superclass.
No. An inner class is something completely different from its outer class. Unless I am misunderstanding your question...
If you define a class subclassing itself as this:
public class Test {
public static void main(String[] args) {
// whatever...
}
class TestChild extends Test {
// whatever...
}
}
Then yes this is possible. But note that these are two entirely separate classes, barring the fact the one is inner to the other.