Jump to content

Inherit from 2 classes


Recommended Posts

I am starting to use inheritance and am facing the following problem:

I would like to inherit from 2 classes.

Here is my project:

post-2399-124936763312_thumb.png

I have a GPS class that reads out a GPS device, using one of two classes: USB and serial.

For storing the data I have two classes as well: TDMS and CSV.

Here is my current hierarchy:

post-2399-124936762448_thumb.png

Is this doable with one class or should I create an 'Input' class for the USB and serial classes and a 'Store' class for the CSV and TDMS classes?

Ton

Link to comment

I am starting to use inheritance and am facing the following problem:

I would like to inherit from 2 classes.

Ton

Hi Ton

I am fairly new to OOP but would still like to suggest the following from my current experiences:

Using the verification statement for inheritance can help:

Ask yourself, or jot down on paper, if the subclass (e.g USB) IS A more specific type of the superclass (e.g. GPS)

USB is a GPS (makes sense from a DAQ-type POV)

Serial is a GPS (makes sense from a DAQ-type POV)

TDMS is a GPS (does not makes sense)

CVS is a GPS (does not makes sense)

Therefore TDMS and CVS should not inherit from GPS. You may want to create another superclass that these two classes inherit from - e.g. a Log Interface - and this class could be have a association with GPS (either through aggregation or composition relationship etc...).

I have been told (in other posts) that design pattern knowledge is the way to go to improve class hierarchy design - this is something I am trying to achieve at the moment.

I have had similar issues as you along the way learning this (as I am sure others have too). Its all part of the fun IMO :)

(Endevo) Mike H mad a recently great post regarding Interfaces in LabVIEW - it is work checking out. And of course AQ's Design Patterns Document on ni.com.

Edited by jgcode
  • Like 1
Link to comment

Daklu,

A delegate pattern can also be used when the class definitions are different.

CP07.GIF

Following scenario would be useful for the problem at hand.

Class A defines the overall functionality required (Loading, Saving, Getting GPS position and so on).

Class A has, as a part of its Data a Class B which defines methods for a GPS hardware module which are used commonly. The actual IMPLEMENTATION of the hardware interfacing is most likely within a child of Class B. By using several different children of Class B (Class B.1, Class B.2) within Class A, we have essentially inherited the interface of the base class (Class B) with a different implementation (Class B.1, Class B.2 and so on).

Class A can also have, as part of its data, a Class C which defined methods for loading and saving. The ACTUAL implementation is again found in a CHILD of Class C (Class C.1, Class C.2). Again we have the functionality within Class A of Class C with the actual implementation defined by the child classes of Class C.

While it is not true multiple inheritance, the effects are similar. We can define two private data member classes (Class B and Class C) which essentially define out interface. Children of these classes can then override the implementation but the method of interacting with these classes (the Interface) is defined by the base class definition.

Class A, instead of inheriting from multiple classes is essentially a COMPOSITE class.

I have used this several times and find it a very powerful way to build modular code.

Shane.

Edited by shoneill
Link to comment

A delegate pattern can also be used when the class definitions are different.

...

Class A has, as a part of its Data a Class B which defines methods for a GPS hardware module which are used commonly. The actual IMPLEMENTATION of the hardware interfacing is most likely within a child of Class B. By using several different children of Class B (Class B.1, Class B.2) within Class A, we have essentially inherited the interface of the base class (Class B) with a different implementation (Class B.1, Class B.2 and so on).

Class A can also have, as part of its data, a Class C which defined methods for loading and saving. The ACTUAL implementation is again found in a CHILD of Class C (Class C.1, Class C.2). Again we have the functionality within Class A of Class C with the actual implementation defined by the child classes of Class C.

If I'm understanding you correctly, what you're describing is the same as the first diagram in my post above, which I agree uses delegation. My comment to AQ was simply to point out that the pattern as described in the article doesn't directly address Ton's problem and it's hard for OOP noobs (especially LVOOP noobs, who often have little formal programming training) to see the various ways delegation can be used to solve problems. If the article were expanded a bit in its description of that pattern it would be much more helpful.

If you look at the LV example referred to in the article it uses delegation in two classes with LV:Object as the common ancestor; however, the class that is delegated to is a concrete class, not an abstract base class. It's having the implementation in the delegate child classes that provides Ton with the flexibility he needs. Does pointing to an abstract base class justify a different pattern name? <*shrug*> I dunno. Several patterns are variations of other patterns, but I'll leave that question to those smarter than me.

Dave

[Edit]

Class A, instead of inheriting from multiple classes is essentially a COMPOSITE class.

How is a composite class different from a facade?

Edited by Daklu
Link to comment

I wasn't sure if you were addressing a "proper" delegate or just the version shown in the documentation, hence my attempt at clarification.

I'm not really up-to-speed on the different OOP patterns. I was using the Delegate pattern before I had ever heard of it, so I'm certainly no authority on the subject. I learn by doing and then some time later I learn how to call what I've been doing....

Differences to a Facade? My understanding (limited as it is) is that the primary role of a facade is to simplify things.

A composite class doesn't necessarily do that. Although a facade is almost certainly also a composite class.

That's about all I know (about OOP patterns).

Shane.

Ps Re-reading your post in light of your new post makes me realise you were actually expanding on the delegate idea instead of offering an alternative. Only realised that now.

Edited by shoneill
Link to comment

I learn by doing and then some time later I learn how to call what I've been doing....

biggrin.gif Me too. There's nothing quite like reinventing the wheel. The thrill of the discovery followed by the realization that not only has someone already invented it, but they did it better than you did.

A composite class doesn't necessarily do that. Although a facade is almost certainly also a composite class.

Good point. There seems to be a lot of subtleties in the jargon.

I'm becoming more and more convinced OO design is something you have to learn by doing it and experiencing all the pitfalls associated with your design decisions. Learning the patterns certainly is necessary... learning which pattern (or pattern variation) to use in certain situations requires a deeper understanding of how the pattern implementation will affect your particular application.

In any event, I'm glad Ton posted the question. I wish there was more OO design related discussion here.

Link to comment

biggrin.gif Me too. There's nothing quite like reinventing the wheel. The thrill of the discovery followed by the realization that not only has someone already invented it, but they did it better than you did.

Amen to that. It's getting over the disappointment the first few times that's hardest.

I'm becoming more and more convinced OO design is something you have to learn by doing it and experiencing all the pitfalls associated with your design decisions. Learning the patterns certainly is necessary... learning which pattern (or pattern variation) to use in certain situations requires a deeper understanding of how the pattern implementation will affect your particular application.

I was taught OOP in college. Well, it wasn't part of the curriculum in my course, but I read up on it anyway. I never ever grasped the idea behind OOP. I never understood how it was supposed to work until I realised that Inheritance and such were COMPILE time events. I had always envisaged changing inheritance at run-time and how this could possibly work really stumped me to the extent that I just gave up on it. With LV 8.20 I just implemented something and suddenly the lights went on.

Something ridiculously silly to get stuck on but none of the resources I had been reading actually explicitly said it was a compile-time event. My assumption was just wrong and cost me about 10 years of OOP understanding. :oops:

I believe the best jargon for that os D'oh!

Shane.

Link to comment

Does pointing to an abstract base class justify a different pattern name? <*shrug*> I dunno.

It turns out the answer is yes. The class diagram I drew up for Ton is a textbook Strategy Pattern.

Apparently Strategy shares topology with the Bridge pattern, the difference being Bridge "allows you to vary the implementation and the abstraction by placing the two in separate class hierarchies." (Head First Design Patterns p.613) How does that distinction affect implementation? I have no idea. I'm willing to be enlightened if anyone has a clue.

Something ridiculously silly to get stuck on but none of the resources I had been reading actually explicitly said it was a compile-time event.

[cheek.insert(tongue)]

You're thinking about this all wrong. You were subconciously applying the idea to an environment (run-time) the authors not only didn't intend, but didn't even anticipate that anyone would attempt, hence none of them ever bothered to explain it is a compile time event. You're thinking outside the box which is a sign of creativity and intelligence. (And ADD too. wink.gif )

If it would have taken you 20 years to figure it out you would be super smart! tongue.gif

Edited by Daklu
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.