Keyword Search and Keyword Selection Example for Assignment #10.
OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass...
Transcript of OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass...
OVERRIDING
7/11/2015 Budditha Hettige ([email protected]) 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 ([email protected]) 83
Example
7/11/2015 Budditha Hettige ([email protected]) 84
Employee
print()
Temporary Employee
print()
Output
7/11/2015 Budditha Hettige ([email protected]) 85
Using the super keyword
• When invoking a superclass version of an
overridden method the super keyword is used.
7/11/2015 Budditha Hettige ([email protected]) 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 ([email protected]) 99
Example FreeTTS
7/11/2015 Budditha Hettige ([email protected]) 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 ([email protected]) 105
Example
7/11/2015 Budditha Hettige ([email protected]) 106