Remember inheritance when a child class inherits methods variables etc from a parent class Example public class Dictionary extends Book Open Book Dictionary InheritanceClient ID: 319406
Download Presentation The PPT/PDF document "Method Overriding" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Method Overriding
Remember
inheritance
: when a child class inherits methods, variables,
etc
from a parent class.
Example:
public class Dictionary extends Book
Open
Book, Dictionary,
InheritanceClient
Note that a client that creates a
Dictionary
object can actually call a method that only exists in the
Book
class.
What if a method exists in both the parent and the child, but has the same exact name?
This is called
method overriding
. The child class overrides its parent’s method, which allows it to accomplish a similar task, but in its own way.Slide2
Polymorphism
What if you know you want to call a method, but you are
not sure yet
if you want to call it from the parent or from the child?
In this case, you can create an object of the parent class, but have it
hold a reference to an object of any of its child classes
. This is called
polymorphism
.
Examples: 1)
Vehicle x = new Car();
2)
List z = new
ArrayList
( );
3)
Vehicle y;
y = new Truck();
Notice that in each of these examples, the order of declarations goes parent
child. Never the reverse.
When an overridden method in one of those classes is then called, how does Java know which one to actually call? Java decides this at run-time. This run-time decision by Java is called
dynamic binding
.Slide3
Polymorphism can technically be defined as
a
mechanism for selecting the correct method for an object in the class
hierarchy.
What is the point of polymorphism?
Often, it makes sense that subclasses (child classes) of
a
superclass (parent class) can
define their own
unique
behaviors
,
and yet share some of the same functionality of the parent class
.
The programmer may wish to call the parent’s method, or one child’s method, or even a different child’s method. (Remember, these are all methods
with the same name
.) So, polymorphism allows that choice to be made
later on
.Slide4
Open:
Animal
Dog
Cat
PolymorphismClientSlide5
So, let’s say we have the following line of code:
Animal k = new Dog();
Or, we have this:
Animal k;
k = new Dog();
Imagine that you write 100,000 lines of code, then decide that
from now on
, you want k to be a Cat object – because there are methods in Cat that do not exist in Dog.
Or, what if the Cat class did not even exist before? You could create it now, and it would not be a problem.
There is no need to go back and change any code. Simply do this:
k
= new Cat();
And now, k is a cat object, with no need to change/fix any code.
This is why polymorphism is so useful
. Slide6
Polymorphism is commonly seen when using
ArrayList
.
ArrayList
is a class that implements the
List
interface. The List interface is a blueprint for its “
implementor
” classes.
There is another
implementor
of the
List
interface, called
LinkedList
, that you do not
have
to know about, but it helps to be aware of it.
LinkedList
is similar to
ArrayList
. It “performs better” than
ArrayList
when using the
add
and
remove
methods, but worse than
ArrayList
when using the
get
and
set
methods.
So, if you plan on inserting/deleting a lot of elements from a list of objects,
LinkedList
is preferable. If you just plan on retrieving elements from a list,
ArrayList
is better.Slide7
Unlike
LinkedList
, you
do
need to be aware of the
List
interface. Often, you will see
ArrayLists
declared this way:
List x;
And then, later on:
x = new
ArrayList
();
Alternatively: you might see this:
List
x =
new
ArrayList
();
Or
, a later line of code might want to
change
x to be a
LinkedList
, like this:
x = new
LinkedList
();
If
x
had
been declared as an
ArrayList
originally, this would be a problem. You would have to
go back and change
a lot of code in order to do this. Using polymorphism here will save you a lot of time.
Note: we learned previously that
an interface cannot be instantiated.
In this example, we are not instantiating List; we are actually instantiating
ArrayList
.
Remember that since List is an interface, its methods are empty. They don’t actually do anything. The whole point of using List is that you can easily switch back and forth from
ArrayList
to
LinkedList
by using polymorphism.Slide8
Restricting the values in an Arraylist
By default, an
ArrayList
can store any type of Object.
You can, if you wish,
restrict
the type of thing that an
ArrayList
can hold, by using the < > symbols. This is called using
generics
.
Examples:
1)
ArrayList
<Person> z = new
ArrayList
<Person>();
2)
ArrayList
<Integer> x
= new
ArrayList
<Integer>();
3) List<String> a;
a = new
ArrayList
<String>();Slide9
Why use generics?
Simply put, using generics will make your code more “stable” by catching more errors at
compile time
rather than at
run-time
.
Compiler errors are usually easier to fix than runtime errors.
When the compiler knows that an
ArrayList
is restricted to holding Strings, for example, it can check your code before running the program to find any errors that specifically involve using Strings.
Another benefit of using generics in an
ArrayList
is that it eliminates the need for type casting
.
Example:
ArrayList
<Integer>
nums
= new
ArrayList
<Integer>();
nums.add
(23);
nums.add
(11); // no need for “new Integer (11)”
etcSlide10
Another example:
public class Sample
{
private
ArrayList
<Comparable>;
// more code here
}
In this class, a private instance variable is created – it is an
Arraylist
.
But note that it will NOT hold objects of the Comparable interface, because that is not valid. An interface cannot be instantiated.
Instead, it will hold objects
that implement the Comparable interface
. Slide11
Static variables
Remember: a static method is a method that can be called from a client without having to create an object. Just use the name of the class itself (examples:
Math.random
()
,
SavitchIn.readLine
()
).
A variable can also be declared with the keyword static.
Just like a static method, a static variable is not accessed through an object. It is accessed by using the name of the class itself.
Why use a static variable? Sometimes,
you want a variable to be shared among all instances of an object
, as opposed to each object having its own version of
that variable.
A static variable is also known as a
class variable
.
Example: see
StaticMethodClass
&
StaticMethodClient
Slide12
Assignment
Go to the AP Exam Prep
Practice Exams folder.
Do
Litvin
Practice Test A2,
#1-20.
Save answers in Word.