Monday, May 31, 2010

Can't come to terms with inheritance

I've been racking my brains over inheritance for a while now, but am still not completely able to get around it.

For example, the other day I was thinking about relating an Infallible Human and a Fallible Human. Let's first define the two:
  • Infallible Human: A human that can never make a mistake. It's do_task() method will never throw an exception

  • Fallible Human: A human that will occasionally make a mistakes. It's do_task() method may occasionally throw a ErrorProcessingRequest Exception

The question was:
IS an infallible human A fallible human OR IS a fallible human AN infallible human?

The very nice answer I received was in the form of a question (I love these since it gives me rules to answer future questions I may have).

"Can you pass an infallible human where a fallible human is expected OR can you pass a fallible human where an infallible human is expected?"

It seems apparent that you can pass an infallible human where a fallible human is expected, but not the other way around. I guess that answered my question.

However, it still feels funny saying "An infallible human is a fallible human". Does anyone else feel queasy when they say it? It almost feels as if speaking out inheritance trees is like reading out statements from propositional calculus in plain English (the if/then implication connectives don't mean the same as that in spoken English). Does anyone else feel the same?

update: This thread on stackoverflow discusses the same issue.

1 comment:

Rakesh Pai said...

This is an interesting thought exercise - something that I've thought about some time ago.

The only logical solution is that classical OO and inheritance in the sense we know today is badly broken. We have to model problems in the way the system can understand, even if it doesn't entirely make sense to us. That's not cool. We need a system that can let us model problems the way we want to.

For example, consider this statement: "Amitabh Bacchan is like Al Pachino as an actor, but Bacchan is taller". Model this statement using classes and inheritance in any sensible aesthetic way, and I'll give it to you. Creating an "AngryYoungPowerfulActor" class that inherits from Actor (though the YoungPowerfulActor Class which itself inherits from the PowerfulActor Class), with a mutable "height" property is just stupid. Where does Nana Patekar fit in here? What about Ajay Devgan? Which class do you inherit from to make perfect sense?

Another one: "The iPad is exactly like the iPhone, but it has a bigger screen". Model this using classes and inheritance. What do you do? Phone > SmartPhone > TouchPhone > PhonesRunningiPhoneOS... no wait - that won't work, since the iPad isn't a phone. Hmm... Devices > DevicesWithOperatingSystems > ...

Classical OO is badly broken. Design patterns is just another testimony to that. And some people think that they are cool cause they use design patterns. Like some people think if they've got the right object hierarchy, they are doing well.