Jump to content

Basic OO question - Overrides & Call Parent Method


Recommended Posts

Hello everyone,

I think I have a fundamental misunderstanding of what the Call Parent Method does, specifically in an override method. Please can someone help make it clear to me?

The first part of the image below shows the contents of my override method when it was created. I thought that this would call the parent's implementation of the method (Close Settings Window) but pass in the override method's class data. However, it doesn't work - it calls the parent method but then seems to use the default parent class data - ie in this case the wrong vi ref.

The seconds part of the image shows what I have ended up implementing in the override method which works fine - it calls the same function that the parent implementation calls. However, it now means I have to edit every override vi to be like this which is duplicating code and adding the risk of errors etc.

I'm guessing I'm missing something or misunderstanding the OO process. Any help would be great.

Thank you,

Martin

post-13935-0-39502300-1317109934_thumb.p

Link to comment

The top solution should work.

Just make sure you have the attribute you need in the parent class, so it can do its job.

Probe the wire before you going to the Parent VI with the sma e name and then also in the parent VI,and you will see that you have the same attrbiute values.

//Mike

Link to comment

I think the issue is that you are expecting that data in the Child Class in being acted when you call Call Parent Method (i.e. your bottom solution) but the data that is actually being acted on is going to be in the Parent Class (i.e. your top solution) - which makes sense as the Parent does not know anything about a Child.

What you need to do is set the data in the Parent Class by calling the appropriate method (assuming that you have such an attribute as you haven't posted class' data members) then call Call Parent Method.

Link to comment

Hi again,

Thank you for the replies. To make sure I understand I've made another screenshot...I've created a class called Vehicle and a child class called car.

- The car:addone method just calls the parent method which does a simple increment on a numeric.

- The value on the probe after the action shows that the behaviour I wasn't expecting - it has two values of Numeric, one for the vehicle and one for the car class. I was expecting it to increment the car class Numeric value not the Vehicle class.

- Does this mean the way I'm doing the override at the moment (from my first post) is the right way of doing things?

Thank you,

Martin

post-13935-0-13835100-1317122567_thumb.p

Link to comment

To make sure I understand I've made another screenshot...I've created a class called Vehicle and a child class called car.

To extend the analogy let's replace your numerics with something vehicles have. A vehicle has an engine, and a method to "Start Engine". You've created a "car", which being a vehicle has an engine that can be started, and you've given it a second engine (!) and were surprised when the "Start Engine" method started the first engine.

OK, so, why did you add a second engine?

-- James

BTW. The "parent/child" terminology of OO is actually confusing; "Car" is not a child of "Vehicle", it is a type of vehicle.

  • Like 1
Link to comment

Private data of a class cannot be overridden, only functions.

In your second example you have a parent Vehicle class with one element Numeric (I'll represent it as Vehicle:Numeric). Then you created a Child (Car) that adds the element Numeric (Car:Numeric). So when look at the instance of Car it has two elements Vehicle:Numeric and Car:Numeric. When you are calling the Parent function of Car to increment Numeric, the parent function is incrementing Vehicle:Numeric since that's the element it knows about and is told to increment. If you want to increment Car:Numeric then you need a Car function.

Note: Since the Data is private to it's class if you want Child classes (or anyone else) to have access to the private data, you will need to implement access functions.

Link to comment

The only thing I'm left wondering is when I would use the default implementation of an override method (ie call parent method), it doesn't seem to be any use apart from as a placeholder.

It serves as a starting point for adding functionality. For example, if your "Car" needs to always have it's headlights on when the engine is on, you would override "Start Engine" and add turning on of the lights to it, while calling the parent method to actually start the engine.

Link to comment

Thank your for all the help on this, it makes better sense now.

The only thing I'm left wondering is when I would use the default implementation of an override method (ie call parent method), it doesn't seem to be any use apart from as a placeholder.

It allows for code reuse. It's use is really going to be dependent on your class structure / project.

Link to comment
The only thing I'm left wondering is when I would use the default implementation of an override method (ie call parent method), it doesn't seem to be any use apart from as a placeholder.
In a huge number of cases, is is just a placeholder. But often it is not. When it is not, it is either
  • a place where you put the initial code where each of the children add additional functionality. For example, the parent might define a "set value.vi"... the child might override to add range limits to that value before calling the parent version to actually set the value.
  • OR it serves as the default when a child doesn't need to do any overriding.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.