Call a child class method from a parent class object

后端 未结 7 531
星月不相逢
星月不相逢 2020-12-06 09:35

I have the following classes

class Person {
    private String name;
    void getName(){...}}

class Student extends Person{
    String class;
    void getCl         


        
相关标签:
7条回答
  • 2020-12-06 10:38

    One possible solution can be

    class Survey{
      void renderSurvey(Question q) {
      /*
          Depending on the type of question (choice, dropdwn or other, I have to render
          the question on the UI. The class that calls this doesnt have compile time 
          knowledge of the type of question that is going to be rendered. Each question 
          type has its own rendering function. If this is for choice , I need to access 
          its functions using q. 
      */
      if(q.getOption() instanceof ChoiceQuestionOption)
      {
        ChoiceQuestionOption choiceQuestion = (ChoiceQuestionOption)q.getOption();
        boolean result = choiceQuestion.getMultiple();
        //do something with result......
      }
     }
    }
    
    0 讨论(0)
提交回复
热议问题