Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic inheritance uses

So if I write the following code in Java:

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

This gives a compiler error message cyclic inheritance involving A.

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

This makes sense to me, as it is quite hard to imagine how this would be possible anyway.

I then asked about this from one of the professors at my uni. He said there are languages where this is possible and he lamented how this is not possible in Java and that he had done some projects where he had used cyclic inheritance and so on, but I couldn't really understand any of it. He also mentioned he had had problems where he would have liked to use cyclic inheritance.

Can you educate me on the possible uses of this strange phenomena of cyclic inheritance? When is it possible and how? Are there problems where this could be useful?

like image 672
Valtteri Avatar asked Nov 19 '12 23:11

Valtteri


People also ask

What is cyclic inheritance?

Cyclic inheritance occurs at outer class A and inner class InnerB because they attempt to inherit from themselves. As for class C, cyclic inheritance occurs at outer class because it inherits from its inner class.

What is cyclic inheritance in Python?

Cyclic Inheritance:Two classes inheriting each other.

Why multiple inheritance is used in interface?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

What is the advantage of interface over inheritance?

One key advantage of interfaces in a single inheritance language is that interfaces can be implemented on classes that do not share a common root. Another point is that interfaces allow what is known as interface inheritance rather than implementation inheritance.


3 Answers

I dug up this interesting reference: basically it says that cyclic inheritance is valid as long as there are no repeated fields, as the lookup for any field just needs to traverse one loop of the cycle to find out a meaning. If a field is repeated then none of the two definitions is more valid than the other and apparently there would be a problem.

So suppose that you want to define a person as a human 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 different aspects of an individual without having to define a hierarchy, and you might instantiate different people as a Person (with name), a Human (that speaks a language) or a Voter (in a particular country). No aspect is more important than the other.

While interesting, I don't think it is practical to use outside of research projects. Imagine having constructors for all classes that pass parameters to the super() constructors -- it would be easy to mess up the whole construct.

Update: the given pseudocode does not compile when translated to Java 8, and apparently under any language (except Cecil as shown by the link given above). It seems that nobody found any valid uses and therefore disallowed cyclic inheritance. This does not mean that the concept is inherently impossible; just that the practical uses do not justify the effort implementing the special case.

like image 177
alexfernandez Avatar answered Sep 29 '22 11:09

alexfernandez


I could only see it possible if the classes were on the same level of hierarchy. Think of the class hierarchy as a tree, which it is. Java looks for the hierarchy to be at least one level above or more. In some languages, you would be able to inherit characteristics from a class on the same level as the class you are using.

like image 33
shaunw Avatar answered Sep 29 '22 10:09

shaunw


I don't get the sense of cyclic inheritance. I don't know why your professor thinks it's useful in anycase mind that the inheritance reation that is called IS-A relationship states that if B is a subclass of A then B IS-A A in the sense that everywhere an A is required then a B can be used without problem (Liskov substitution principle).

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

In addition you will have many circumstanced in which odd side effects comes into play (think about method A.foo() calling super.foo() and B.foo() calling super.foo(). I don't see any practical reason because this should be allowed.

Inheritance is intended as a tree in which every subclass specifies the behavior or the classes that are up in the tree, having two classes at the same level doesn't mean anything useful.

like image 40
Jack Avatar answered Sep 29 '22 10:09

Jack