Using circular inheritance

So, if I write the following code in Java:

public class A extends A{ public static void main(String[] args){ } } 

This gives circular compiler error inheritance using A.

The same thing happens if I write two classes A and B and A inherits B and B inherits A.

This makes sense to me, as it’s hard to imagine how this is possible in any case.

Then I asked one of the professors of my youth about this. He said that there are languages ​​where this is possible, and he lamented how impossible it is in Java, and that he completed some projects in which he used cyclic inheritance and so on, but I could not understand this. He also mentioned that he had problems when he would like to use circular inheritance.

Can you tell me about the possible uses of these strange phenomena of cyclic inheritance? When is this possible and how? Are there problems when this can be useful?

+6
source share
5 answers

I dug up this interesting link : basically it says that cyclic inheritance is valid if there are no duplicate fields, since the search for any field just needs to go through one cycle of the cycle to find out the meaning. If the field is repeated, then neither of the two definitions is more reliable than the other, and there will probably be a problem.

Suppose you want to define a person as a person and as a voter, and set different attributes for each. In pseudo code:

 class Person extends Human: String name; class Human extends Voter: String language; class Voter extends Person: Country residence; 

Now you can address various aspects of the personality without having to define a hierarchy, and you can create different people as a Person (with a name), a Person (who speaks a language), or a Voter (in a specific country). No aspect is more important than other.

Although interesting, I do not think it is practical to use outside of research projects. Imagine that for all classes that pass parameters to super () constructors, it would be easy to spoil the whole construct.

Update: this pseudo code does not compile when switching to Java 8 and, apparently, to any language (except Cecil, as shown by the link above). No one seems to have found any valid uses, and therefore has forbidden cyclic inheritance. This does not mean that the concept is inherently impossible; that practical use does not justify efforts to implement a special case.

+5
source

I do not agree with the accepted answer, which says that their code will not cause an error. I run Java 8 and compiled the following code:

 class Person extends Human: String name; class Human extends Voter: String language; class Voter extends Person: String residence; 

and I got the error message "error: circular inheritance involving Person".

Therefore, in Java you cannot have circular inheritance.

+2
source

I only saw this if the classes were on the same hierarchy level. Think of the class hierarchy as the tree that it is. Java looks for a hierarchy at least one level higher or more. In some languages, you can inherit characteristics from a class at the same level as the class you are using.

0
source

I do not understand the meaning of circular inheritance. I don’t know why your professor considers it useful in any case that inheritance inheritance, called IS-A , states that if B is a subclass of A, then B IS-A A in the sense that everywhere A is required, then you can use B without a problem (Liskov substitution principle).

Now, theoretically, if A is a subclass of B and B is a subclass of A, then both classes should have exactly the same external interface. This is because if you add a method to any of them, the other will inherit the same method automatically, so you will either have to override it or get a different implementation.

In addition, you will have many circumstances in which the odd side effects come in (think of the A.foo() method calling super.foo() and B.foo() calling super.foo() . I don't see any practical reasons, because it should be allowed.

Inheritance is intended as a tree, in which each subclass defines a behavior or classes that are in the tree, having two classes at the same level, does not mean anything useful.

0
source

Cyclical inheritance is useless, and besides, let's see why this is logically forbidden.

I would like to start with the Object class, if the class does not extend any class, it extends the Object class (this is true), and if the class extends any other class, it indirectly extends the Object class.

Example:

 class B { ..// } class A extends B { ...// } 

class A extends Object , because class B extends the class Object .

So, when we execute Cyclic Inheritance , it never extends the Object class.

Example:

 class A extends A { } 

Hope I was clear, and a class that cannot extend the Object class, which is not possible in Java.

0
source

All Articles