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
7/11/2015 Budditha Hettige (budditha@yahoo.com) 84
Employee
print()
Temporary
Employee
print()
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
87
Budditha Hettige
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
88
Budditha Hettige
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();
89
Employee.java: xx: Employee is abstract; cannot be instantiated
Employee e = new Employee();
^ 1 error1
Budditha Hettige
Extending Abstract Class
public class Salary extends Employee
{
private double salary;
Salary(String name, String address,
int number, double salary)
{
super(name, address, number);
setSalary(salary);
}
...
}
90
Extend Employee
class
Call Employee
class
Budditha Hettige
Abstract Methods
• Can declare the method in the parent class as
abstract
• Abstract methods consist of a method signature, but
no method body
91
Budditha Hettige
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
92
Budditha Hettige
Example
93
Budditha Hettige
Packages
94
Budditha Hettige
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
95
Budditha Hettige
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
96
Budditha Hettige
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
97
Budditha Hettige
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.*;
98
Budditha Hettige
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
101
Budditha Hettige
Budditha Hettige
102
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!).
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
103
Budditha Hettige
Types of Polymorphism
• Overloading
• Overriding
• Dynamic method binding
104
Budditha Hettige
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 105
Example
7/11/2015 Budditha Hettige (budditha@yahoo.com) 106

04_-note voerrding java code simapl-oop.pdf

  • 1.
  • 2.
    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
  • 3.
    Example 7/11/2015 Budditha Hettige(budditha@yahoo.com) 84 Employee print() Temporary Employee print()
  • 4.
    Output 7/11/2015 Budditha Hettige(budditha@yahoo.com) 85
  • 5.
    Using the superkeyword • When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige (budditha@yahoo.com) 86
  • 6.
  • 7.
    Abstraction • Refers tothe 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 88 Budditha Hettige
  • 8.
    Abstract Class • Usethe abstract keyword to declare a class abstract public abstract class Employee { private String name; private String address; ... } Cannot use Employee e = new Employee(); 89 Employee.java: xx: Employee is abstract; cannot be instantiated Employee e = new Employee(); ^ 1 error1 Budditha Hettige
  • 9.
    Extending Abstract Class publicclass Salary extends Employee { private double salary; Salary(String name, String address, int number, double salary) { super(name, address, number); setSalary(salary); } ... } 90 Extend Employee class Call Employee class Budditha Hettige
  • 10.
    Abstract Methods • Candeclare the method in the parent class as abstract • Abstract methods consist of a method signature, but no method body 91 Budditha Hettige
  • 11.
    Declaring a Methodas 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 92 Budditha Hettige
  • 12.
  • 13.
  • 14.
    Package • defined asa 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 95 Budditha Hettige
  • 15.
    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 96 Budditha Hettige
  • 16.
    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 97 Budditha Hettige
  • 17.
    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.*; 98 Budditha Hettige
  • 18.
    FreeTTS Example • Wayto use existing packages – Add jar library – Import class 7/11/2015 Budditha Hettige (budditha@yahoo.com) 99
  • 19.
    Example FreeTTS 7/11/2015 BuddithaHettige (budditha@yahoo.com) 100
  • 20.
  • 21.
    Budditha Hettige 102 Polymorphism • Thereal 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!).
  • 22.
    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 103 Budditha Hettige
  • 23.
    Types of Polymorphism •Overloading • Overriding • Dynamic method binding 104 Budditha Hettige
  • 24.
    Example 7/11/2015 Budditha Hettige(budditha@yahoo.com) 105
  • 25.
    Example 7/11/2015 Budditha Hettige(budditha@yahoo.com) 106