inheritance in java. rhs – soc 2 what is inheritance (in java)? inheritance is a mechanism for...

27
Inheritance in Java

Upload: oliver-griffin

Post on 01-Jan-2016

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

Inheritance in Java

Page 2: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 2

What is inheritance (in Java)?

• Inheritance is a mechanism for enhancing existing classes

• What does that mean…?

• Defining new classes, which build on the function-ality of existing classes

Page 3: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 3

What is inheritance (in Java)?

• Suppose we have a BankAccount class, which provides basic functionality common for all types of bank accounts– Depositing money– Withdrawing money– Retrieving the balance

• But most bank accounts have more functionality that just this…

Page 4: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 4

What is inheritance (in Java)?

BankAccount

Checking AccountMonthly feeTransaction fee

Savings AccountMonthly interestUpper balance limit

Page 5: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 5

What is inheritance (in Java)?BankAccount

- balance

+ deposit+ withdraw+ getBalance()

CheckingAccount

- chargeMonthlyFee()- chargeTransFee()

SavingsAccount

- rate

- depositInterest()- checkBalanceLimit()

Page 6: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 6

Inheritance in code

public class SavingsAccount extends BankAccount

{

private double rate;

public SavingsAccount(double rate) {...}

public void depositInterest() {...}

public bool checkBalanceLimit() {...}

}

Page 7: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 7

Inheritance

• We only need to define the new methods and instance fields for SavingsAccount

• Methods and instance fields from BankAccount are inherited

• We extend the BankAccount class without touching it (code reuse)

• ”Closed for modification, open for extension”

Page 8: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 8

InheritanceBankAccount

- balance

+ deposit+ withdraw+ getBalance()

SavingsAccount

- rate

- depositInterest()- checkBalanceLimit()

Superclass

Subclass

Page 9: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 9

Inheritance

• Why is the class with most functionality called the subclass?

• Terminology from set theory

BankAccount

SavingsAccount

CheckingAccount

Page 10: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 10

Inheritance vs. Interfaces

• Related, but not the same

• If you must implement an interface, you are ”ordered” to implement a set of certain methods

• If you extend a class, you get something ”for free”

Page 11: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 11

Inheritance vs. Interfaces

• One quite important difference between inheritance and interfaces:– A class can implement multiple interfaces– A class can only extend one class– There is no such thing as multiple inheritance

in Java…– Multiple inheritance has issues, taken out of

Java to keep things simple

Page 12: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 12

Inheriting methods

• A subclass has three options when defining methods:– Inherit methods as-is– Override methods– Define new methods

(just as we are used to)

Page 13: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 13

Inheriting methods

• Inherit methods as-is

• Just as it sounds – the method will work exactly as it works in the superclass

• Methods can (still) be applied to objects of the superclass, and also to objects of the subclass

Page 14: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 14

Inheriting methods

• Override methods

• We can actually provide a different imple-mentation of a method from the superclass

• Superclass may provide a reasonable default implementation

• Subclasses may substitute it for a more useful implementation

Page 15: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 15

Inheriting methods

public class DefaultShape

{

public void draw() { // do nothing }

public double getArea() { return 0.0;}

}

Page 16: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 16

Inheriting methods

public class Circle extends DefaultShape

{

// new instance fields, etc.

public void draw() // Override

{

// code for drawing a Circle

}

public double getArea() // Override

{

return (radius*radius*Math.PI);

}

}

Page 17: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 17

Inheriting methods

public class Point extends DefaultShape

{

// new instance fields, etc.

public void draw() // Override

{

// code for drawing a Point

}

// However, I keep implementation of getArea

}

Page 18: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 18

Inheriting methods

DefaultShape ds = new DefaultShape();

double area1 = ds.getArea();

Circle c = new Circle(1,1,1);

double area2 = c.getArea();

Point p = new Point(1,1);

double area3 = p.getArea();

DefaultShape dsc = new Circle(2,2,2);

double area4 = ds.getArea();

Page 19: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 19

Inheriting methods

• ”In Java, method calls are always determined by the type of the actual object, not the type of the object reference”

• This is polymorphism

Page 20: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 20

Inheriting methods

• What if we want to ”supple-ment” a method, not override it completely

• Do we need to implement all of the code in the super-class again?

• No – and it would break encapsulation!

Page 21: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 21

Inheriting methods

// Original method from BankAccount

public void deposit(double amount)

{

balance = balance + amount;

}

// Overridden method in CheckingAccount

public void deposit(double amount)

{

balance = balance + amount;

transactionCount++;

}

Ooops!

Page 22: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 22

Inheriting methods

// Overridden method in CheckingAccount

public void deposit(double amount)

{

super.deposit(amount);

transactionCount++;

}

• ”Do what the superclass does in deposit, then do my stuff”

New keyword!

Page 23: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 23

Inheriting methods

• We can also do this for constructors!

// Constructor for subclass

public CheckingAccount(double initialBalance)

{

super(initialBalance);

// Now do initialisation specific for

// the CheckingAccount class

transactionCount = 0;

}

Page 24: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 24

Conversions

• Rules for conversion between super- and sub-class are similar to conversion rules between interfaces and implementation class– Always legal to convert to a superclass– Legal – but risky – to cast from superclass to

subclass

Page 25: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 25

Conversions

• In other words:

DefaultShape s1 = new Square(10,10,10); // OK

DefaultShape s2 = new Point(20,20); // OK

Circle c = new Circle(5,10,20); // OK

DefaultShape s = new DefaultShape(); // OK (!)

Circle c = new DefaultShape(); // NO!

Square sq = new Circle(5,20,40); // NO!

Page 26: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 26

Conversions

• And this is still dangerous!

public void enlarge(DefaultShape s)

{

Circle c = (Circle)s;

double r = c.getRadius();

c.setRadius(2*r);

}

OK – if s is a Circle!

Page 27: Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining

RHS – SOC 27

Exercises

• Self-check: 1, 3, 10, 11

• Review: R10.3, R 10.7

• Programming: P10.4