Is deeply nested inheritance bad or good practice?

I am creating a PHP web application. I have a situation that, in my opinion, would contribute to 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 will be a bad design practice; however, I am not an advanced object oriented programmer by any means. With that said, before I want to use this code in my application, I would like to know if this is a good or bad practice for more experienced / skilled programmers.

NOTE. I changed the header from multiple inheritance to nested inheritance after fixing the wrong term.

thank

Steve

+3
source share
5 answers

To quote David West :

Then the definition of inheritance becomes: “A subordinate-subordinate relationship between classes in which the subordinate (child) has the same behavioral (duties) as senior (parent) plus at least one additional.

So what you do seems fine if you add behavior to your derived classes (i.e., inheritance is not a tool to add additional members) and (preferably) without changing the specific behavior of the base classes.

Development

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

, , . .

( OOP Object Thinking.)

+4

RecurringEvent, , , , n ( , ).

, , ? , ( ) , (, , ..).

, , - . RecurringWeeklyEvent RecurringEvent, Event. RecurrungWeeklyEvent RecurringEvent Event ( , php java).

+2

. - , /. , , - run-of-them-mill, , , . , , . .

+2

, " ".

, WeeklyRecurringEvent. , ( TwoWeeklyRecurringEvent, MonthlyRecurringEvent), . :

, PHP, Java, , :


class RecurringEvent extends Event{
   RecurringStep step;

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

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

, factory:


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

, factory RecurringEvent.

+1
source

I would call it nested inheritance. In any case, do not try to find the problem here. You just stop nested inheritance when you don't like it anymore.

0
source

Source: https://habr.com/ru/post/1726629/


All Articles