Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Inheritance - accumulating properties

Subject: Inheritance - accumulating properties

From: Sven

Date: 25 Oct, 2013 19:45:22

Message: 1 of 1

Hi all,

I have a small set of classes that form some hierarchy such as below:

classdef Human
    properties
        Abilities = {'walk'}
    end
end

classdef Athlete < Human
    properties
        Abilities = {'run'}
    end
end

classdef Hurdler < Athlete
    properties
        Abilities = {'jump'}
    end
end

I can then instantiate some objects, and list their abilities.

Tom = Human();
Mary = Hurdler();
fprintf('%s\n', Tom.Abilities{:}) % Produces 'walk'
fprintf('%s\n',Mary.Abilities{:}) % Produces 'jump'

I recognise that the nature of inheritance is that you overload or replace previous properties with your new property. However, what I'd really like to do is have:

fprintf('%s\n',Mary.Abilities{:})

... to actually list all of the abilities from all of the classes that Mary has inherited, so that her "Abilities" are actually 'walk', 'run', 'jump', rather than simply 'jump'.

I find that I am hitting this sort of design problem quite frequently. Is there a particular inheritance pattern that I can use to produce the desired "cumulative inheritance" effect?

Thanks,
Sven.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us