Inheritance
Daniel Archambault
CS-115: Inheritance
1
Previously in CS-115
Get the input from files too
CS-115: Inheritance
2
Previously in CS-115
Is opening files similar to System.in
CS-115: Inheritance
3
Previously in CS-115
Is opening files similar to System.in How do we open a file for reading?
CS-115: Inheritance
3
Previously in CS-115
Is opening files similar to System.in How do we open a file for reading? How do we open one for writing?
CS-115: Inheritance
3
Previously in CS-115
Is opening files similar to System.in How do we open a file for reading? How do we open one for writing? How do we change the delimiter?
CS-115: Inheritance
3
Previously in CS-115
Is opening files similar to System.in How do we open a file for reading? How do we open one for writing? How do we change the delimiter?
Do we need to close file streams and how do we do it?
CS-115: Inheritance
3
Previously in CS-115
Objects are good, but sometimes can cause repeated information How can we overcome this?
Inheritance
CS-115: Inheritance
4
Inheritance
Is-a relationship
a mammal is an animal
a bird is a animal
a car is a vehicle
an aeroplane is a vehicle.
Animal and vehicle are superclasses
superclasses are a general class type
Mammal, bird, car, and aeroplane are subclasses
subclasses are more specific versions of the superclass.
CS-115: Inheritance
5
Example Inheritance Hierarchy
CS-115: Inheritance
6
Motivation for Inheritance
Consider an online store…
class Book
private double price; private String author; private int pages; private int numStock; private String title;
class DVD
private double price; private String director; private int numStock; private double dur; private String title;
class CD
private double price; private int numStock; private String artist; private double dur; private String title;
class Teacup
private double price; private int numStock; private float vol;
CS-115: Inheritance
7
Motivation for Inheritance (2)
In this design, lots of duplication
Can lead to bugs if we change in one class but not all
class Book
private double price; private String author; private int pages; private int numStock; private String title;
class DVD
private double price; private String director; private int numStock; private double dur; private String title;
class CD
private double price; private int numStock; private String artist; private double dur; private String title;
class Teacup private double price; private int numStock; private float vol;
CS-115: Inheritance
8
Motivation for Inheritance (3)
We can factor out common behaviour and attributes, placing in super classes
All subclasses inherit all of the information of the superclass
class DVD
private String director;
class Product private double price; private int numStock;
class Media private String title;
class Book
private String author; private int pages;
class Playable private double dur;
class Teacup
private float vol;
class CD
private String artist;
CS-115: Inheritance
9
Motivation for Inheritance (4)
Information is inherited for all classes above on a path A DVD has: price, numStock, title, duration, artist.
class Product private double price; private int numStock;
class Media private String title;
class Teacup
private float vol;
class Book
private String author; private int pages;
class Playable private double dur;
class DVD
private String director;
class CD
private String artist;
CS-115: Inheritance
10
Creating Inheritance Using extends
The extends keyword creates inheritance relationships public class DVD extends Playable {
public class CD extends Playable {
class Playable private double dur;
class DVD
private String director;
class CD
private String artist;
CS-115: Inheritance
11
Benefits of Inheritance
All data and methods in one location
makes maintenance easier
avoids reimplementation of features
Data and operations available to all subclasses Exceptions in Java form an inheritance hierarchy
CS-115: Inheritance
12
Superclass
Provides methods and attributes common to all subclasses References can store instances of subclasses
Methods could implement default behaviours unless overridden by subclass
class Playable private double dur;
class DVD
private String director;
class CD
private String artist;
CS-115: Inheritance
13
Subclass
Inherits the methods and attributes of the superclass
Additional methods and attributes can be added to the subclass Methods in the superclass can be overridden
done by giving the methods the exact same declarations You can call public (and protected) methods of superclass
class Playable private double dur;
class DVD
private String director;
class CD
private String artist;
CS-115: Inheritance
14
What is protected?
Private means no calling outside class in which defined (bummer). But, the data exists if you are a subclass.
Is there a way to access this data directly? protected
protected means than any subclass can access the data directly
that is, any class on the extends path in the hierarchy
class Playable protected double dur;
class DVD
private String director;
Now anything that extends Playable can access dur
class CD
private String artist;
CS-115: Inheritance
15
What is super?
super refers to the superclass of a class
it is a keyword in Java that you can use when you need to do this super can be used as a reference
super.myMethod () runs myMethod in the superclass
Usually, you want to run the constructor of the superclass when constructing a subclass so that you can reuse initialisation code
super (…) calls the superclass constructor, just specify the right parameters
https://docs.oracle.com/javase/tutorial/java/
IandI/super.html
CS-115: Inheritance
16
Access Quiz
Ball extends sphere
Sphere has an attribute protected int radius; Sphere has a method public void throwIt () Ball has a method protected void bounce () Ball has a method public void throwIt ()
We are inside the ball class. Is the call legal? What method is called?
this.radius;
this.throwIt ();
Outside sphere and ball
Ball b = new Ball (…..);
Sphere s = new Ball (…..);
s.radius;
b.radius;
s.throwIt ();
b.throwIt ();
b.bounce ();
CS-115: Inheritance
17