Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance and Polymorphism - Ease of use vs Purity

In a project our team is using object lists to perform mass operations on sets of data that should all be processed in a similar way. In particular, different objects would ideally act the same, which would be very easily achieved with polymorphism. The problem I have with it is that inheritance implies the is a relationship, rather than the has a relationship. For example, several objects have a damage counter, but to make this easy to use in an object list, polymorphism could be used - except that would imply an is a relationship which wouldn't be true. (A person is not a damage counter.)

The only solution I can think of is to have a member of the class return the proper object type when implicitly casted instead of relying on inheritance. Would it be better to forgo the is a / has a ideal in exchange for ease of programming?

Edit: To be more specific, I am using C++, so using polymorphism would allow the different objects to "act the same" in the sense that the derived classes could reside within a single list and be operated upon by a virtual function of the base class. The use of an interface (or imitating them via inheritance) seems like a solution I would be willing to use.

like image 910
Cristián Romo Avatar asked Aug 15 '08 01:08

Cristián Romo


People also ask

What is the difference between polymorphism and inheritance?

Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). Whereas polymorphism is that which can be defined in multiple forms. 2. It is basically applied to classes.

How do inheritance and polymorphism related?

Inheritance is a property pertaining to just classes whereas, polymorphism extends itself into any method and/or function. Inheritance allows the derived class to use all the functions and variables declared in the base class without explicitly defining them again.

Is polymorphism the same with composition?

Composition involves aggregating multiple objects to form a single entity. Polymorphism involves multiple objects that share analogous behavior.

Is polymorphism a composition?

Polymorphism is the ability of a specific chemical composition to crystallize in more than one form. This generally occurs as a response to changes in temperature or pressure or both. The different structures of such a chemical substance are called polymorphic forms, or polymorphs.


2 Answers

I think you should be implementing interfaces to be able to enforce your has a relationships (am doing this in C#):

public interface IDamageable
{
    void AddDamage(int i);
    int DamageCount {get;}
}

You could implement this in your objects:

public class Person : IDamageable

public class House : IDamageable

And you'd be sure that the DamageCount property and has a method to allow you to add damage, without implying that a person and a house are related to each other in some sort of heirarchy.

like image 166
Jon Limjap Avatar answered Sep 22 '22 02:09

Jon Limjap


This can be accomplished using multiple inheritance. In your specific case (C++), you can use pure virtual classes as interfaces. This allows you to have multiple inheritance without creating scope/ambiguity problems. Example:

class Damage {
    virtual void addDamage(int d) = 0;
    virtual int getDamage() = 0;
};

class Person : public virtual Damage {
    void addDamage(int d) {
        // ...
        damage += d * 2;
    }

    int getDamage() {
        return damage;
    }
};

class Car : public virtual Damage {
    void addDamage(int d) {
        // ...
        damage += d;
    }

    int getDamage() {
        return damage;
    }
};

Now both Person and Car 'is-a' Damage, meaning, they implement the Damage interface. The use of pure virtual classes (so that they are like interfaces) is key and should be used frequently. It insulates future changes from altering the entire system. Read up on the Open-Closed Principle for more information.

like image 24
Brian Avatar answered Sep 21 '22 02:09

Brian