title: How to implement multilevel inheritance using design pattern
body: <p>I had previously implemented Abstract Factory Pattern on simple problems and it worked. So I tried to use same thing to solve this problem but I was confused. I wrote the bottom level classes but confused how to combine them to a single program. What Should I do and How should it be done?</p>
  
  <p>I am using Java to write a code to calculate tax. I am having base class <code>TaxPayer</code>. Taxpayer can have multiple <code>incomeSource</code>. There can be many types of <code>TaxPayer</code> or <code>IncomeSource</code>. There can be many income headings for different income sources as its attributes, which will be stored in database. The tax rate will be different for different taxpayer type and amount of <code>taxableIncome</code>. </p>
  
  <p>Base class Taxpayer is defined as </p>
  
  <pre><code>public abstract class TaxPayer {
      private List&lt;IncomeSource&gt; incomeSource;
      double taxRate;
      Address address;
      other attributes here;
  
      public Double getTaxRate(){
          return 0.25; //default tax rate
      }
  }
  
  public abstract class IncomeSource {
      private String incomeSourceName;
      private Double incomeHeading1, incomeHeading2, incomeHeading3;
      private Double totalIncome = incomeHeading1 + incomeHeading2 + incomeHeading3;
  }
  </code></pre>
  
  <p>There can be more levels of <code>IncomeSource</code> inheritance with different income headings. Similarly tax payer type can be modeled into following inheritance structure</p>
  
  <pre><code>Base Class: Taxpayer
      * IndividualPerson
          * Male, Female, OldAge
      * Business
          * Bank, ITIndustry, HydroElectricIndustry
      * TaxFree
          * SocialOrganization, ReligiousOrganization, PoliticalParty etc.
  </code></pre>
  
  <p>The subclasses of <code>TaxPayer</code> generally modifies the <code>taxRate</code> to be applied to <code>taxableIncome</code> and sometimes changes <code>taxableIncome</code> with some logic. For an example:</p>
  
  <pre><code>abstract class IndividualPerson extends TaxPayer{
      if (incomeSource.taxableIncome &gt; 250000) taxRate = ratex;
      if (incomeSource.taxableIncome &gt; 500000) taxRate = ratey;
      @override
      public getTaxRate() {
          return taxRate;
      }
  }
  class Female extends IndividualPerson {
      if (incomeSource.getNumberOfIncomeSource() &gt; 1) taxRate = taxRate + rate1;
      else taxRate = taxRate - rate2
      if (address.isRural() = true) taxRate = taxRate - rate3;
      if (attributeX = true) taxRate = taxRate + rate4;
      if ("Some other attribute" = true) taxableIncome = taxableIncome - someAmount;
  }
  </code></pre>
  
  <p>We have to check other attributes of <code>Taxpayer</code> and <code>IncomeSource</code> to determine the <code>taxRate</code>. Mostly, <code>taxRate</code> is different for different logic, but sometimes, <code>taxableIncome</code> can be discounted.</p>
  
  <p>I am trying return tax rate according to TaxPayer type and taxableIncome. I am confused how to combine bottom level classes together.</p>