Search code examples
javaparametersconstructorsubclasssuperclass

A subclass type as parameter type in superclass constructor java


So I have a node class and I haven't learned Linked List yet, so I can't use that. To construct a node object I want the parameters to be like this:

Node(int numberOfNode, type complex or simpel)

I have two subclasses of node called Simpelnode and complexNode and so a node object can be either one of them. What do I need to do so that the parameter can be of both types?


Solution

  • Node(int numberOfNode, Node node)
    

    Since SimpleNode and ComplexNode are both subclasses of Node, a SimpleNode is a Node, and a ComplexNode is a Node. So using Node as the argument type will allow passing a SimpleNode as well as a ComplexNode.