OVERRIDING
7/11/2015     Budditha Hettige (budditha@yahoo.com)   82
What is Overriding
• Is a language feature
• Allows a subclass or child class to provide a
  specific implementation of a method that is already
  provided by one of its super classes or parent
  classes.
• The implementation in the subclass overrides
  (replaces) the implementation in the superclass by
  providing
7/11/2015   Budditha Hettige (budditha@yahoo.com)   83
Example
                                                    Employee
                                                     print()
                                                    Temporary
                                                    Employee
                                                     print()
7/11/2015   Budditha Hettige (budditha@yahoo.com)              84
Output
7/11/2015   Budditha Hettige (budditha@yahoo.com)   85
Using the super keyword
• When invoking a superclass version of an
  overridden method the super keyword is used.
7/11/2015   Budditha Hettige (budditha@yahoo.com)   86
Abstraction
    Budditha Hettige
                       87
Abstraction
• Refers to the ability to make a class abstract in OOP
• Abstract class
  – Cannot be instantiated
  – Other functionality of the class still exists
  – Cannot create an instance of the abstract class
                        Budditha Hettige
                                                    88
Abstract Class
• Use the abstract keyword to declare a class
  abstract
  public abstract class Employee
  {
     private String name;
     private String address;
    ...
  }
  Cannot use
    Employee e = new Employee();
      Employee.java: xx: Employee is abstract; cannot be instantiated
      Employee e = new Employee();
      ^ 1 error1
                                    Budditha Hettige
                                                                        89
Extending Abstract Class
public class Salary extends Employee
{                                           Extend Employee
  private double salary;                          class
  Salary(String name, String address,
           int number, double salary)
  {
      super(name, address, number);
      setSalary(salary);
  }                              Call Employee
                                      class
  ...
}
                           Budditha Hettige
                                                              90
Abstract Methods
• Can declare the method in the parent class as
  abstract
• Abstract methods consist of a method signature, but
  no method body
                       Budditha Hettige
                                                   91
Declaring a Method as Abstract
• The class must also be declared abstract
• Any child class must either override the abstract
  method or declare itself abstract
   – A child class that inherits an abstract method
     must override it
   – If they do not, they must be abstract, and any of
     their children must override it
                        Budditha Hettige
                                                     92
Example
          Budditha Hettige
                             93
Packages
  Budditha Hettige
                     94
Package
• defined as a grouping of related types
• existing packages in Java are:
   – java.lang - bundles the fundamental classes
   – java.io - classes for input , output functions are
     bundled in this package
                         Budditha Hettige
                                                          95
Creating a Package
• Put a package statement with the package name
• At the top of every source file that contains the
  classes, interfaces, enumerations, and annotation
  types that you want to include in the package
• The package statement should be the first line in
  the source file
• There can be only one package statement in each
  source file, and it applies to all types in the file
                        Budditha Hettige
                                                         96
Example
  /* File name : Animal.java */
  package animals;
  interface Animal
  {
     public void eat();
  }
  package animals;
  /* File name : MammalInt.java */
  public class Mammal implements Animal
  {
  public void eat()
  {
  System.out.println("Mammal eats");
  }
    Now you compile these two files and put them
    in a sub-directory called animals
                          Budditha Hettige
                                                   97
The Import Keyword
• If a class wants to use another class in the same
  package, the package name does not need to be
  used. Classes in the same package find each other
  without any special syntax
• The fully qualified name of the class can be used.
   – import animals.Mamal
• The package can be imported using the import
  keyword and the wild card (*)
   – import animals.*;
                       Budditha Hettige
                                                  98
FreeTTS Example
• Way to use existing packages
  – Add jar library
  – Import class
7/11/2015   Budditha Hettige (budditha@yahoo.com)   99
Example FreeTTS
7/11/2015   Budditha Hettige (budditha@yahoo.com)   100
Polymorphism
    Budditha Hettige
                       101
Polymorphism
• The real power comes with methods/behaviors.
• A better example:
   – shape object types used by a drawing program.
   – we want to be able to handle any kind of shape
     someone wants to code (in the future).
   – we want to be able to write code now that can
     deal with shape objects (without knowing what
     they are!).
                       Budditha Hettige
                                                  102
Example
   public interface Vegetarian {}
   public class Animal{}
   public class Deer extends Animal implements Vegetarian{}
• Deer class is considered to be polymorphic since this has
  multiple inheritance
   – A Deer IS-A aAnimal
   – A Deer IS-A Vegetarian
   – A Deer IS-A Deer
   – A Deer IS-A Object
                              Budditha Hettige
                                                              103
Types of Polymorphism
• Overloading
• Overriding
• Dynamic method binding
                     Budditha Hettige
                                        104
Example
7/11/2015   Budditha Hettige (budditha@yahoo.com)   105
Example
7/11/2015   Budditha Hettige (budditha@yahoo.com)   106