Jump over parent constructor to call grandparent's

后端 未结 10 1827
遥遥无期
遥遥无期 2020-12-18 22:27

The problem is this: I have an abstract class that does some work in its constructor, and a set of child classes that implement the abstract class:

class Abs         


        
10条回答
  •  Happy的楠姐
    2020-12-18 22:47

    You cannot do this in Java. I frequently have students who want to do this, but I have never seen a case where it is really what they wanted to do.

    Can you give a concrete example of what it is you want to do and why (your description is too vague) and I am sure a solution can be achieved :-)

    Edit:

    For a real world example of why you do not want to do this (normally) would be a hierarchy like:

    Animal (constructor makes the eyes)
      |
    Mammal (constructor makes the lungs)
      |
    Human (constructor sets the language)
    

    If the Human constructor could skip over the Mammal constructor then you would wind up with a Human who has no lungs... not very useful.

提交回复
热议问题