views:

249

answers:

6

I am in the process of making a PHP web application. I have a situation that I believe would foster a good time for nested inheritance. Anyway, here is my situation:

public class RecurringWeeklyEvent extends RecurringEvent { }

public class RecurringEvent extends Event { }

It does not seem to me that this would be a bad design practice; however, I am not an advanced Object-Oriented programmer by any means. With that said, before I venture off using this kind of code in my application, I would like to know if this is a good or bad practice from more experienced/qualified programmers.

NOTE: I changed the title from multiple inheritance to nested inheritance after being corrected of using the wrong term.

Thanks

Steve

+2  A: 

The best way, would be to have the RecurringEvent, and each object has information on when it recurs, probably every n days (or even hours, minutes).

From these two lines, I'm guessing you're making a calendar? You will probably want to make a different class (sub class of recurring event) for events that recur in non standard ways (for example, third monday of the week, first monday in October, and the like).

And just so you know, this isn't multiple inheritance, multiple inheritance is when a single class has more than one direct parent class. In this case, RecurringWeeklyEvent extends RecurringEvent which extends Event. With multiple inheritance, it would be RecurrungWeeklyEvent extends RecurringEvent and Event (I'm not sure the syntax to do this in php or java).

Jeffrey Aylesworth
@Jeffrey - there is no syntax for multiple inheritance in Java. It is not supported.
Stephen C
A: 

I would call it nested inheritance maybe. Either way don't try to find a problem here. You just stop nesting inheritance when you don't feel comfortable with it anymore.

Sergej Andrejev
+2  A: 

multiple inheritance is actually something a bit different. Multiple inheritance is where your class inherits from multiple interfaces / superclasses. What you have is a regular run-of-them-mill class hierarchy which I believe is perfectly fine. I find that sometimes one can get carried away with creating overly complex object hierarchies, so just be aware of that. Your design looks acceptable to me though.

darren
+3  A: 

To quote David West:

The definition of inheritance then becomes, "A superordinate-subordinate relationship between classes in which the subordinate (child) has the same behaviours (responsibilities) as the superordinate (parent) plus at least one additional."

So what you're doing seems fine, so long as you're adding behaviour to your derived classes (i.e., inheritance is not a tool for adding in extra members), and (preferably) not altering the defined behaviour of the bases classes.

Elaboration

What duffymo says is correct. I want to add my 2p from my interpretation of what David West says in his book.

Overriding methods should be avoided where possible. Changing the behaviour of a method in a child class could lead to confusion for implementers.

(I'm still getting my head around OOP and Object Thinking.)

Matt Ellen
Hi Matt, can you elaborate a bit on what you mean by behaviour? Does this mean that a class should only be derived if that class has added/overridden methods and not only additional members?
stjowa
"behavior" means methods. Either you add new methods or override existing ones.
duffymo
A: 

I think you would be better served changing the title to "Deeply nested inheritence - Bad or Good?"

slebetman
You should really use a comment for suggestions like these, rather than posting it as an answer. *(Seeing as this is a comment, not an answer ;-)*
Atli
+1  A: 

I wouldn't call it nested inheritance, just 'Deep inheritance tree'.

Looking at your example I wouldn't create a WeeklyRecurringEvent. It is inflexible for extension if you add more time steps (you then would create classes TwoWeeklyRecurringEvent, MonthlyRecurringEvent), which gets confusing and end up with a lot of dedicated classes. For that I would skip inheritance and a more composed approach:

The last time I wrote PHP is a while ago, I just present the idea in Java, should be enough to get the idea:


class RecurringEvent extends Event{
   RecurringStep step;

   RecurringEvent(RecurringStep step){
     this.step=step;
   }
}

enum RecurringStep{
  MONTHLY,WEEKLY,DAILY,YEARLY;
}

For making it easier to create recurring events inside code I maybe would also offer factory methods:


class RecurringEventFactory{
...
  static createDailyEvent(){
     return new RecurringEvent(DAILY);
  }
...
}

Of course you could encapsulate factory methods directly to the RecurringEvent class.

manuel aldana