title: Does the example below violate Liskov Substitution Principal?
body: <p>Could anybody tell me the example below violate LSP or not?</p>
  <p>I have an example:</p>
  <pre><code>public class Person {
      private String name;
      private Integer age;
  
      public Person(String name, Integer age) {
          this.name = name;
          this.age = age;
      }
  
      public void validate() {
          if (age == null || age &lt; 0) {
              throw new IllegalArgumentException(&quot;Age can not be null&quot;);
          }
      }
  }
  </code></pre>
  <p>and the subclass:</p>
  <pre><code>public class Employee extends Person {
      private String employeeCode;
  
      public Employee(String name, Integer age, String employeeCode) {
          super(name, age);
          this.employeeCode = employeeCode;
      }
  
      @Override
      public void validate() {
          super.validate();
          if (employeeCode == null) {
              throw new IllegalArgumentException(&quot;Employee code can not be null&quot;);
          }
      }
  }
  </code></pre>
  <p>and the main class:</p>
  <pre><code>public class Main {
      public static void main(String[] args) {
          Person person = new Person(&quot;Person&quot;, 10);
          validate(person); // will be ok. does not throw any exception
  
          Person employee = new Employee(&quot;Employee&quot;, 30, null);
          validate(employee); // will be throw IllegalArgumentException because subtype's employee code is null
      }
  
      public static void validate(Person p) {
          p.validate();
      }
  }
  
  
  </code></pre>
  <p>In this example the subclass add new property called <code>employeeCode</code> and override method <code>validate</code>
  with additional check for it's own property <code>employeeCode</code>.</p>
  <p>In the main method I create 2 objects. The first one is object of type <code>Person</code> and the second one is object of type <code>Employee</code>.</p>
  <p>When validate person it is ok because all precondition is ok
  but for the employee it will throw an <code>IllegalArgumentException</code> because it does not match precondition</p>
  <ol>
  <li>Does <code>Employee</code> violate LSP due to add new validation on <code>employeeCode</code>?</li>
  <li>If 1 is <em><strong>yes</strong></em>, how can I refactor it to avoid violate LSP?</li>
  <li>If 1 is <em><strong>no</strong></em>, if I change exception from <code>IllegalArgumentException(&quot;Employee code can not be null&quot;)</code> to another exception <code>NullPointerException</code>. So does it violate LSP due to introduce new exception type in subtype (which super type does not have)?</li>
  </ol>