Object oriented programming (OOP): Inheritance...

Post on 21-May-2020

28 views 0 download

Transcript of Object oriented programming (OOP): Inheritance...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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