Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a child class method from a parent class object

Tags:

I have the following classes

class Person {     private String name;     void getName(){...}}  class Student extends Person{     String class;     void getClass(){...} }  class Teacher extends Person{     String experience;     void getExperience(){...} } 

This is just a simplified version of my actual schema. Initially I don't know the type of person that needs to be created, so the function that handles the creation of these objects takes the general Person object as a parameter.

void calculate(Person p){...} 

Now I want to access the methods of the child classes using this parent class object. I also need to access parent class methods from time to time so I CANNOT MAKE IT ABSTRACT.


I guess I simplified too much in the above example, so here goes , this is the actual structure.

class Question {   // private attributes   :   private QuestionOption option;   // getters and setters for private attributes   :   public QuestionOption getOption(){...}  }   class QuestionOption{  ....  }  class ChoiceQuestionOption extends QuestionOption{  private boolean allowMultiple;  public boolean getMultiple(){...}  }   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().getMultiple())         {...}   }  } 

The if statement says "cannot find getMultiple for QuestionOption." OuestionOption has many more child classes that have different types of methods that are not common among the children (getMultiple is not common among the children)

like image 816
user1349663 Avatar asked Jul 13 '12 08:07

user1349663


People also ask

How do you call a method of parent class by an object of a child class in Java?

Use of super with methods This is used when we want to call the parent class method. So whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword.

How do you call a child class method from parent class object in Python?

Calling Parent class Method Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.

Can I access a parent class method by a child class object?

It has all of the instance variables. The only unusual aspect is that, within child class method definitions, you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.


1 Answers

NOTE: Though this is possible, it is not at all recommended as it kind of destroys the reason for inheritance. The best way would be to restructure your application design so that there are NO parent to child dependencies. A parent should not ever need to know its children or their capabilities.

However.. you should be able to do it like:

void calculate(Person p) {     ((Student)p).method(); } 

a safe way would be:

void calculate(Person p) {     if(p instanceof Student) ((Student)p).method(); } 
like image 77
techfoobar Avatar answered Oct 12 '22 10:10

techfoobar