overriding - wordpress.com · 3/4/2015 · using the super keyword •when invoking a superclass...
TRANSCRIPT
![Page 2: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/2.jpg)
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
![Page 3: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/3.jpg)
Example
7/11/2015 Budditha Hettige ([email protected]) 84
Employee
print()
Temporary Employee
print()
![Page 5: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/5.jpg)
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
![Page 6: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/6.jpg)
Abstraction
87 Budditha Hettige
![Page 7: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/7.jpg)
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
![Page 8: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/8.jpg)
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
![Page 9: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/9.jpg)
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
![Page 10: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/10.jpg)
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
![Page 11: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/11.jpg)
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
![Page 12: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/12.jpg)
Example
93 Budditha Hettige
![Page 13: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/13.jpg)
Packages
94 Budditha Hettige
![Page 14: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/14.jpg)
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
![Page 15: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/15.jpg)
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
![Page 16: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/16.jpg)
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
![Page 17: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/17.jpg)
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
![Page 18: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/18.jpg)
FreeTTS Example
• Way to use existing packages
– Add jar library
– Import class
7/11/2015 Budditha Hettige ([email protected]) 99
![Page 20: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/20.jpg)
Polymorphism
101 Budditha Hettige
![Page 21: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/21.jpg)
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!).
![Page 22: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/22.jpg)
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
![Page 23: OVERRIDING - WordPress.com · 3/4/2015 · Using the super keyword •When invoking a superclass version of an overridden method the super keyword is used. 7/11/2015 Budditha Hettige](https://reader033.vdocuments.us/reader033/viewer/2022060308/5f0a37c67e708231d42a94fb/html5/thumbnails/23.jpg)
Types of Polymorphism
• Overloading
• Overriding
• Dynamic method binding
104 Budditha Hettige