object oriented programming (oop): inheritance...

149
Overview Bicycle inheritance Scope Shape inheritance Polymorphism Object oriented programming (OOP): Inheritance and polymorphism Prof. Dionne Aleman MIE250: Fundamentals of object-oriented programming University of Toronto MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 1 / 51

Upload: others

Post on 21-May-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Object oriented programming (OOP):Inheritance and polymorphism

Prof. Dionne Aleman

MIE250: Fundamentals of object-oriented programmingUniversity of Toronto

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 1 / 51

Page 2: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Inheritance1

When classes have a lot in common, we can modularize common featuresusing inheritance.

1Bicycle example from Oracle’s Java TutorialsMIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 2 / 51

Page 3: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The superclass (nothing special)

1 public class Bicycle {2 private int numGears;3 private double price;45 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }1415 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 3 / 51

Page 4: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass MountainBike

Only need members and methods that are different from the superclass

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 4 / 51

Page 5: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass MountainBike

Only need members and methods that are different from the superclass

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 4 / 51

Page 6: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass MountainBike

Only need members and methods that are different from the superclass

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 4 / 51

Page 7: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass MountainBike

Only need members and methods that are different from the superclass

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 4 / 51

Page 8: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass RoadBike

Only need members and methods that are different from the superclass

1 public class RoadBike extends Bicycle {23 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }78 }

src/BikeInheritance/RoadBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 5 / 51

Page 9: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass RoadBike

Only need members and methods that are different from the superclass

1 public class RoadBike extends Bicycle {23 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }78 }

src/BikeInheritance/RoadBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 5 / 51

Page 10: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass TandemBike

Only need members and methods that are different from the superclass

1 public class TandemBike extends Bicycle {2 private int nRiders;34 public TandemBike () {5 super();6 this.nRiders = 2;7 }89 public TandemBike(int n) {

10 super();11 this.setNRiders(n);12 }1314 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }1617 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.javaMIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 6 / 51

Page 11: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The subclass TandemBike

Only need members and methods that are different from the superclass

1 public class TandemBike extends Bicycle {2 private int nRiders;34 public TandemBike () {5 super();6 this.nRiders = 2;7 }89 public TandemBike(int n) {

10 super();11 this.setNRiders(n);12 }1314 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }1617 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.javaMIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 6 / 51

Page 12: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What exactly is passed from parent to child?

I A subclass inherits all of the public and protected members of itsparent (superclass), no matter what package the subclass is in.

I A class inherits fields and methods from all its superclasses, whetherdirect or indirect.

I A subclass can override methods that it inherits.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 7 / 51

Page 13: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Particulars

I Each class can have one direct superclass only.

I Each superclass can have infinite subclasses.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 8 / 51

Page 14: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A note on @Override

I Optional, though some IDEs will flag if not used

I Will definitely be flagged if used incorrectly

I Good to use to let you know that a method is supposed to overridea superclass method

I If the superclass method changes later, the compiler will let youknow that something isn’t right

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 9 / 51

Page 15: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle inheritance exampleWe’ve already seen the superclass and the subclass. Let’s use them.

1 public class myBikes {23 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);89 Bike.setPrice (250);

10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");1415 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }2021 }

src/BikeInheritance/myBikes.javaMIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 10 / 51

Page 16: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Using inherited classes

I Exactly the same as regular classes!

I The only thing different is in writing the subclasses.

I So, we can save ourselves a lot of time and effort by just inheritingfrom other classes that have a lot of overlap with what we want ournew classes to do.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 11 / 51

Page 17: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Go through it line-by-line

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 12 / 51

Page 18: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Go through it line-by-line

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 12 / 51

Page 19: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Go through it line-by-line

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to Bicycle constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 12 / 51

Page 20: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle Bike in main → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 13 / 51

Page 21: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle Bike in main → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 13 / 51

Page 22: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle Bike in main → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 13 / 51

Page 23: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle Bike in main → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 13 / 51

Page 24: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bicycle Bike in main → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Go back to main←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 13 / 51

Page 25: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 14 / 51

Page 26: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 14 / 51

Page 27: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to MountainBike constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 14 / 51

Page 28: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 15 / 51

Page 29: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 15 / 51

Page 30: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 15 / 51

Page 31: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

Go to Bicycle constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 15 / 51

Page 32: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 16 / 51

Page 33: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 16 / 51

Page 34: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 16 / 51

Page 35: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 16 / 51

Page 36: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor → Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Go back to MountainBike constructor←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 16 / 51

Page 37: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 17 / 51

Page 38: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 17 / 51

Page 39: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 17 / 51

Page 40: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MountainBike MtnBike in main → MountainBikeconstructor

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

Go back to main←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 17 / 51

Page 41: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MountainBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 18 / 51

Page 42: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MountainBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 18 / 51

Page 43: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MountainBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to RoadBike constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 18 / 51

Page 44: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike constructor

No specific constructor, so use the parent’s constructor!

1 public class RoadBike extends Bicycle {2

3 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }7

8 }

src/BikeInheritance/RoadBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 19 / 51

Page 45: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike constructor

No specific constructor, so use the parent’s constructor!

1 public class RoadBike extends Bicycle {2

3 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }7

8 }

src/BikeInheritance/RoadBike.java

Go to Bicycle constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 19 / 51

Page 46: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike → Bicycleconstructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 20 / 51

Page 47: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike → Bicycleconstructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 20 / 51

Page 48: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike → Bicycleconstructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 20 / 51

Page 49: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike → Bicycleconstructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 20 / 51

Page 50: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RoadBike RdBike in main → RoadBike → Bicycleconstructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Go back to main←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 20 / 51

Page 51: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 21 / 51

Page 52: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 21 / 51

Page 53: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bicycle constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to TandemBike constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 21 / 51

Page 54: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 55: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

There are two constructors ...Which to use?

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 56: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Not this constructor!The arguments don’t match!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 57: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

This constructor,since it takes the same arguments

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 58: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 59: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Go to Bicycle constructor→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 22 / 51

Page 60: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor→ Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 23 / 51

Page 61: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor→ Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 23 / 51

Page 62: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor→ Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 23 / 51

Page 63: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor→ Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 23 / 51

Page 64: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor→ Bicycle constructor

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Go back to TandemBike constructor←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 23 / 51

Page 65: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 66: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 67: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Go to function setNRiders()in this class

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 68: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 69: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Set this.nRiders = 4

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 70: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TandemBike TdmBike in main → TandemBike constructor1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Go back to main←

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 24 / 51

Page 71: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from TandemBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 25 / 51

Page 72: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from TandemBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 25 / 51

Page 73: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from TandemBike constructor

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to Bike’s setPrice() function→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 25 / 51

Page 74: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bike.setPrice(250) in main → Bicycle’s setPrice()function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 26 / 51

Page 75: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bike.setPrice(250) in main → Bicycle’s setPrice()function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Set this.price = 250 andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 26 / 51

Page 76: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 27 / 51

Page 77: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 27 / 51

Page 78: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to MtnBike’s setPrice() function→

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 27 / 51

Page 79: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setPrice(375) in main → MountainBike’ssetPrice() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 28 / 51

Page 80: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setPrice(375) in main → MountainBike’ssetPrice() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

No setPrice() function here,so go to parent’s setPrice()

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 28 / 51

Page 81: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setPrice(375) in main → MountainBike class→ Bicycle’s setPrice() function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 29 / 51

Page 82: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setPrice(375) in main → MountainBike class→ Bicycle’s setPrice() function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Set this.price = 375 andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 29 / 51

Page 83: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 30 / 51

Page 84: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 30 / 51

Page 85: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setPrice()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to MtnBike’ssetSuspension() function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 30 / 51

Page 86: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setSuspension("Hard tail") in main →MountainBike’s setSuspension() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 31 / 51

Page 87: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.setSuspension("Hard tail") in main →MountainBike’s setSuspension() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

Set this.suspension = "Hard tail"and go back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 31 / 51

Page 88: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 89: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 90: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

No! There is no suchfunction in Bicycle!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 91: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 92: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

No! There is no suchfunction in TandemBike!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 93: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 94: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.setSuspension()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to Bike’srideTheMountain() function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 32 / 51

Page 95: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bike.rideTheMountain() in main → Bicycle’srideTheMountain() function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 33 / 51

Page 96: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Bike.rideTheMountain() in main → Bicycle’srideTheMountain() function

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance/Bicycle.java

Print message andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 33 / 51

Page 97: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 34 / 51

Page 98: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 34 / 51

Page 99: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from Bike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to MtnBike’srideTheMountain() function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 34 / 51

Page 100: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.rideTheMountain() in main →MountainBike’s rideTheMountain() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 35 / 51

Page 101: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

MtnBike.rideTheMountain() in main →MountainBike’s rideTheMountain() function

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 @Override // indicates that this method overrides the superclass17 public void rideTheMountain () {18 System.out.println("All systems w/in normal operating parameters.");19 }20 }

src/BikeInheritance/MountainBike.java

Print message andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 35 / 51

Page 102: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 36 / 51

Page 103: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 36 / 51

Page 104: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from MtnBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to RdBike’srideTheMountain() function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 36 / 51

Page 105: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RdBike.rideTheMountain() in main → RoadBike’srideTheMountain() function

1 public class RoadBike extends Bicycle {2

3 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }7

8 }

src/BikeInheritance/RoadBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 37 / 51

Page 106: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

RdBike.rideTheMountain() in main → RoadBike’srideTheMountain() function

1 public class RoadBike extends Bicycle {2

3 @Override4 public void rideTheMountain () {5 System.out.println("Severe damage sustained.");6 }7

8 }

src/BikeInheritance/RoadBike.java

Print message andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 37 / 51

Page 107: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from RdBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 38 / 51

Page 108: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from RdBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 38 / 51

Page 109: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from RdBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

Go to TdmBike’srideTheMountain() function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 38 / 51

Page 110: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TdmBike.rideTheMountain() in main → TandemBike’srideTheMountain() function

1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 39 / 51

Page 111: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

TdmBike.rideTheMountain() in main → TandemBike’srideTheMountain() function

1 public class TandemBike extends Bicycle {2 private int nRiders;3

4 public TandemBike () {5 super();6 this.nRiders = 2;7 }8

9 public TandemBike(int n) {10 super();11 this.setNRiders(n);12 }13

14 public int getNRiders () { return this.nRiders; }15 public void setNRiders(int n) { this.nRiders = n; }16

17 @Override18 public void rideTheMountain () {19 System.out.println(this.nRiders + " people stuck in a ditch.");20 }21 }

src/BikeInheritance/TandemBike.java

Print message andgo back to main

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 39 / 51

Page 112: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from TdmBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 40 / 51

Page 113: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Back from TdmBike.rideTheMountain()

1 public class myBikes {2

3 public static void main(String [] args) {4 Bicycle Bike = new Bicycle ();5 MountainBike MtnBike = new MountainBike ();6 RoadBike RdBike = new RoadBike ();7 TandemBike TdmBike = new TandemBike (4);8

9 Bike.setPrice (250);10 MtnBike.setPrice (375);11 MtnBike.setSuspension("Hard tail");12 Bike.setSuspension("Hard tail");13 TdmBike.setSuspension("Hard tail");14

15 Bike.rideTheMountain ();16 MtnBike.rideTheMountain ();17 RdBike.rideTheMountain ();18 TdmBike.rideTheMountain ();19 }20

21 }

src/BikeInheritance/myBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 40 / 51

Page 114: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Who sees what in inheritance?

Modifier Within class Outside class, insame package

Outside class,outside package

public X X X

protected X X

private X

Since we won’t deal with packages in this class, for our purposes, publicand protected are the same. So, let’s just ignore protected.2

2In most other OOP languages, protected is heavily used in inheritance situations.MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 41 / 51

Page 115: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Who sees what in inheritance?

I We know that if a member variable or method is private, it can’tbe seen in the main or elsewhere.

I Can a private member variable or method in a superclass be seenby its subclasses?

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 42 / 51

Page 116: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Who sees what in inheritance?

I We know that if a member variable or method is private, it can’tbe seen in the main or elsewhere.

I Can a private member variable or method in a superclass be seenby its subclasses?

No!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 42 / 51

Page 117: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 118: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 119: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 120: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 121: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 122: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 123: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 124: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 125: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 126: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

A scope example

1 public class valueBikes {2

3 public static void main(String [] args) {4 MountainBike MtnBike = new MountainBike ();5 MtnBike.setPrice (405);6 MtnBike.setTrailRating (2.5);7

8 if (MtnBike.isGoodValue ())9 System.out.println("Good value!");

10 else11 System.out.println("Bad value!");12 }13

14 }

src/BikeInheritance_scope/valueBikes.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 43 / 51

Page 127: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The same Bicycle class

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance_scope/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 44 / 51

Page 128: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The same Bicycle class

1 public class Bicycle {2 private int numGears;3 private double price;4

5 public Bicycle () {6 this.numGears = 0;7 this.price = 0.0;8 }9

10 public double getPrice () { return this.price; }11 public void setPrice(double price) { this.price = price; }12 public int getGears () { return this.numGears; }13 public void setGears(int numGears) { this.numGears = numGears; }14

15 public void rideTheMountain () {16 System.out.println("Popped a tire.");17 }18 }

src/BikeInheritance_scope/Bicycle.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 44 / 51

Page 129: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 130: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

The new function

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 131: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 132: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 133: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 134: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

Allowable?

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 135: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

The new MountainBike class

1 public class MountainBike extends Bicycle {2 private String suspension;3 private double trailRating;4

5 public MountainBike () {6 super(); // calls superclass constructor7 this.suspension = "Downhill";8 this.trailRating = 2;9 }

10

11 public double getTrailRating () { return this.trailRating; }12 public void setTrailRating(double t) { this.trailRating = t; }13 public String getSuspension () { return this.suspension; }14 public void setSuspension(String s) { this.suspension = s; }15

16 public boolean isGoodValue () {17 if (this.trailRating > 2 && this.price < 400)18 return true;19 return false;20 }21 }

src/BikeInheritance_scope/MountainBike.java

No!price is private

to Bicycle!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 45 / 51

Page 136: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

So what should we do instead?

Keep in mind we don’t want to just make price public.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 46 / 51

Page 137: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?

I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 138: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sides

I Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 139: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)

I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 140: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 141: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?

I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 142: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate area

I Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 143: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)

I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 144: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)

I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 145: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

What if we had a geometry program that managed a lot ofshapes?

I What are common features different shapes have?I Number of sidesI Number of dimensions (2D or 3D)I Name

I What are common things we would do with a shape?I Calculate areaI Calculate perimeter (only if 2D)I Calculate volume (only if 3D)I Print out name

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 47 / 51

Page 146: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Perfect time for inheritance

Let’s build a superclass and a few subclasses for our shape managementprogram.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 48 / 51

Page 147: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Polymorphism

I Polymorphism is a lot like method overloading, but with inheritedmethods.

I Instead of having a different method for different inputs, we canhave a different method for different calling objects. In other words,many related classes with the same method name.

I We’ve already been doing it!

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 49 / 51

Page 148: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Polymorphism in the Bicycle example1 public class Bicycle {2 ...3 public void rideTheMountain () {4 System.out.println("Popped a tire.");5 }6 }789 public class MountainBike extends Bicycle {

10 ...11 public void rideTheMountain () {12 System.out.println("All systems w/in normal operating parameters.");13 }14 }151617 public class RoadBike extends Bicycle {18 ...19 public void rideTheMountain () {20 System.out.println("Severe damage sustained.");21 }22 }

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 50 / 51

Page 149: Object oriented programming (OOP): Inheritance …morlab.mie.utoronto.ca/MIE250/notes/MIE250_07-OOP2.pdf1Bicycle example from Oracle’s Java Tutorials MIE250: Fundamentals of object-oriented

Overview Bicycle inheritance Scope Shape inheritance Polymorphism

Polymorphism in the Bicycle example

I Even though the superclass Bicycle has a rideTheMountain()method, each subclass has its own version.

I So, the actual method run depends on whether the method is calledby a Bicycle object, a MountainBike object, a RoadBike object,etc.

I It’s not the input arguments that determine which method is run (asin regular overloading), but the calling object.

MIE250: Fundamentals of object-oriented programming (Aleman) OOP: Inheritance and polymorphism 51 / 51