Chapter 8: Here`s some notes on inheritance

Download Report

Transcript Chapter 8: Here`s some notes on inheritance

Chapter 8
Improving Structure with Inheritance
The DoME Example



The Database of Multimedia Entertainment
We will be storing information about CDs and
videos
We will want to be able to perform actions on
each of these lists of items.
Desired Functionality




Enter information about CDs and videos
Store information permanently
Search functionality
Print lists of all CDs or videos
Desired Information






Title of album
Artist
Number of tracks
Total Playing time
“Got it” flag
Comment





Title of video
Name of director
Total Playing time
“Got it” flag
Comment
Design Discussion




Each of the private fields would have a setter
and a getter
The each list of CDs and videos could be
stored in a separate array list
Do you see any issues with this approach?
DoME Source Code
Major Issue





Code duplication
There is code duplication in almost every
aspect of every class
With the two ArrayLists we have to duplicate
every method.
With repeated information we have to repeat
the same code in both the CD and video
class
What happens when we want to add books?
Inheritance




Inheritance allows us to define one class as
an extension of another
Inheritance allows subclasses to get all the
fields and methods from the superclass.
In this way, we can put all the common
information in one class and inherit from it
Then we can put the specialized information
in the subclasses
Better Classes for DoME
Item
title
playingTime
gotIt
comment
setComment
getComment
setOwn
getOwn
print
CD
artist
numberOfTracks
getArtist
getNumberOfTracks
Video
director
getDirector
Inheritance Hierarchies
Animals
Mammal
Dog
Poodle
Bird
Cat
Dalmatian
Chicken
Sparrow
Inheritance and Java




There are some syntax issues when dealing
with Java and inheritance
We use the keyword extends to indicate that
one class inherits from another class
We may choose to use different access
modifiers within the base class to allow for
easier access in the child classes
We need ways to initialize the super class
Inheritance and Java
public class Item
{
private String
title;
private int
playingTime;
private boolean
gotIt;
private String
Comment;
…
}
public class CD
extends Item
{
private String
artist;
private int
numberOfTracks;
…
}
Inheritance and Access Rights




Just like any other class a subclass of a
superclass is not allowed to access any
private data; it must use setters and getters
It does not need to use a dot in front of any
methods
It can call them as if they were their own
We will come back to this issue when later
and introduce a protected access modifier
Inheritance and Initialization



When we create an object the constructor is
called and the state of the object is set.
But how do we set the state of the
superclass?
In the first line of the subclass’s constructor
we use the key word super and supply any
necessary parameters to the superclass’s
constructor
Example
public class Item
{
…
public Item( String
theTitle, int time )
{
title = theTitle;
playingTime = time;
gotIt = false;
comment = “”;
}
…
}
public class CD extend Item
{
…
public CD( String
theTitle, String
theArtist, int tracks, int
time )
{
super(theTitle, time);
artist = theArtist;
numberOfTracks =
tracks;
}
…
}
Things to Notice



The constructor for the CD receives the
information necessary to create an item in
addition to the information need to create the
CD
The key word super is the call to Item’s
constructor and we pass in the information it
wants
The first line of the subclass’s constructor
must always be to the superclass’s
constructor through the key word super
Advantages of Inheritance




Avoiding code duplication
Code reuse
Easier maintenance
Extensibility
Subtyping




Let’s take a look at the Database code since
we have changed to inheritance
DoME Example V2
It is much simpler
The reason is subtyping
Subtyping Again




When we have said in the past that when you
call a method, the actual parameters must
match the type of the formal parameters
We could have said must match the type or
be a subtype of the formal paramaters
We can now have one ArrayList that will hold
items
So addItem will take an item or any of its
subclasses as a parameter.
Subtyping and Assignments







We are allowed to assign to
a variable any object that
matches its type or is a
subtype of its type
These are allowed
Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();
Vehicle v3 = new Bike();
This is not
Car c1 = new Vehicle();
Vehicle
Car
Bike
Polymorphism



Polymorphism, for now, is an idea that allows
a variable to be able to be more than one
thing at different times.
It allows us to simplify our print code for the
database.
We will come back to this important idea later
The Object class






In Java all objects inherit from the Object class.
We don’t have to do anything, it is automatic.
This is how ArrayLists can hold any type of object.
They expect an Object as their parameters.
Since all objects inherit from this common base, we
can pass any object to a collection in Java
That is also why, it gives us an object back and we
have to cast it back to its particular type.
HW

Chapter 8: 8.3, 8.6, 8.11, 8.13, 8.17