Software Engineering I SENG201
Lecture 12 – Inheritance (part 2) March 16, 2022
Copyright By PowCoder代写 加微信 powcoder
Previous lecture
1. Introduction to inheritance
2. Overriding methods
3. Subclass constructors
1. Substitutability and conversion
2. Interfaces and inheritance
3. Abstract classes
1. Substitutability and conversion
2. Interfaces and inheritance
3. Abstract classes
“Substitutability” and conversion
• Can convert specific to generic
– myVehicle1 can only use parent functions • myVehicle1.printVehicleID() ok
• myVehicle1.printBrand() not ok
– WhatifCaroverridesprintVehicleID()? • myVehicle1 uses overridden version
• Cannot convert generic to specific
– But: can cast myVehicle1 to “real” Car
Car myCar1 = new Car();
Vehicle myVehicle1 = myCar1;
// Same as: Vehicle myVehicle1 = new Car();
printVehicleID() setVehicleID() getVehicleID()
printBrand()
Vehicle myVehicle2 = new Car();
Car myCar2 = myVehicle2; // compiler error Car myCar2 = (Car) myVehicle2; // OK
Example use case of conversions – array lists
• Array list of vehicles can store cars, but also bikes
– Cars and bikes in list can use behavior defined for vehicles
printVehicleID() setVehicleID() getVehicleID()
printBrand()
ArrayList
Car myCar1 = new Car(); Bike myBike1 = new Bike();
vehicle.add(myCar1); vehicle.add(myBike1);
vehicle.get(0).printVehicleID();
vehicle.get(1).printVehicleID();
vehicle.get(0).printBrand();
// ok, prints ID of car
// ok, prints ID of bike
// not ok, no brand in vehicle
Another example
Rectangle Square
public class Square extends Rectangle {
public void setWidth(int aWidth) {
super.setWidth(aWidth); super.setHeight(aWidth);
public void setHeight(int aHeight) {
super.setWidth(aHeight); super.setHeight(aHeight);
Overwritten methods (setWidth() + setHeight() ) have changed behavior
public class Tester {
public static void main(String[] args) {
Rectangle r = new Square(); r.setWidth(5);
r.setHeight(10); System.out.println(r.getArea());
public class Rectangle {
private int
private int
public void
public void
setWidth(int aWidth) width = aWidth;
setHeight(int aHeight) height = aHeight;
public int getWidth() {
return width; public int getHeight()
return height; public int getArea()
return width * height;
Liskov Substitution Principle (LSP):
“Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.”
1. Substitutability and conversion
2. Interfaces and inheritance
3. Abstract classes
Multiple interfaces
• Single inheritance – can only have one parent – May not have single “right” choice
<
start() stop() refuel()
<
cruiseSpeed()
maxAltitude()
runwayLength()
CivilianVehicle MilitaryVehicle
Cessna Canoe Carriage
Battleship
public class CheckingAccount extends BankAccount implements Measurable {
private int transactionCount;
// implement method from Measurable
public void getMeasure() {
return balance;
// use or override method from superclass
public void deposit(double amount) {
transactionCount++;
super.deposit(amount);
1. Substitutability and conversion
2. Interfaces and inheritance
3. Abstract classes
Classes – interfaces – abstract classes
– We know what behavior / methods exist
– Weknowhowbehavior/methodsareimplemented
• Interfaces
– We know what behavior / methods exist
– We do not know how concrete implementations of behavior look like
• Abstract classes
– Weknowwhatbehavior/methodsexist
– For some behavior / methods we know generic implementation
– For some behavior / methods we cannot define generic implementation
– Genericimplementationswouldneverapplytoanysubclass
• All animals “eat”
– Dogs and fishes “eat” in a similar way
– Define generic implementation of eat() in a class Animal
• All animals move
– Dogs and fishes move in very different ways
– Only prescribe that move() must exist in class Animal
– Leave implementation of move() to subclasses of Animal
– Agenericimplementationofmove()wouldneverbeused
Properties
concrete Methods
+abstract Methods
Abstract class
Properties
concrete Methods
abstract Methods
At least one abstract method Cannot be instantiated
Example – concrete BankAccount
public class BankAccount {
private double balance;
public void withdraw(double aAmount)
balance = balance – aAmount;
public double getBalance() {
return balance;
public void setBalance(double aValue) {
balance = aValue;
Example –abstractBankAccount
public abstract class BankAccount {
private double balance;
public abstract void withdraw(double aAmount);
public double getBalance() {
return balance;
public void setBalance(double aValue) {
balance = aValue;
Example – putting it together
public class CheckingAccount extends BankAccount {
private int numberTransactions;
public void withdraw(double aAmount) // abstract method in BankAccount
numberTransactions++;
setBalance(getBalance() – aAmount);
public class SavingsAccount extends BankAccount {
public void withdraw(double aAmount) // abstract method in BankAccount {
setBalance(getBalance() – aAmount);
Example in UML
• Denoted by italicising names
BankAccount
-balance: double
+withdraw(aAmount: double): void
+getBalance(): double
+setBalance(aValue: double): void
SavingsAccount
CheckingAccount
-numberTransactions: int
1. Substitutability and conversion
2. Interfaces and inheritance
3. Abstract classes
Cartoon of the day
Key lesson: Abstract classes combine features of “concrete” classes and interface classes.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com