usb usb - 1 year ago 123
Java Question

Cyclic inheritance hierarchy in Java

I know following cyclic inheritance hierarchy is not allowed in Java. Compiler throws an error, but what I'm really interested is knowing the exact reason for the compilation failure.

class A extends B{}
class B extends C{}
class C extends A{} // this will give you compile time error.

What is the thing due to which the compiler will throw an error, the moment I write the code
class C extends A{}

Answer Source

Such relation is simply not possible. It defines an infinite recursive class. In order to define class C, you need class A, to define class A you need class B, and to define class B you need class C - and you are back to the starting point. This goes on infinitely so compiler can't do this and it also has no logical meaning.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download