·  JAVA – Syntax – Terms – Overriding

 

- Info:

  - Overridden methods have the same signature but are redefined within class that extends.

 

- Example:

  - In this example we define some default attack behaviour characteristic for all Persons.

  - Then we override this behaviout for Soldier by giving him a gun.

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

      public static void main(String[] args) {

        Soldier john = new Soldier();

                john.attack();       

      }     

    }

    

    class Person {

      void attack() { System.out.println("Using fist."); }

    }

    

    class Soldier extends Person {

      void attack() { System.out.println("Using gun.");  }

    }