Inheritance Check out Inheritance from SVN Sometimes a new class is - - PowerPoint PPT Presentation

inheritance
SMART_READER_LITE
LIVE PREVIEW

Inheritance Check out Inheritance from SVN Sometimes a new class is - - PowerPoint PPT Presentation

Inheritance Check out Inheritance from SVN Sometimes a new class is a a speci cial al ca case of the concept represented by another Can borrow from an existing class, changing just what we need The new class inher erits


slide-1
SLIDE 1

Inheritance

Check out Inheritance from SVN

slide-2
SLIDE 2
slide-3
SLIDE 3

 Sometimes a new class is a

a speci cial al ca case of the concept represented by another

 Can “borrow” from an

existing class, changing just what we need

 The new class inher

erits its from the existing one:

  • all methods
  • all instance fields

Q1

slide-4
SLIDE 4

 class SavingsAccount extends BankAccount

  • adds interest earning, keeps other traits

 class Employee extends Person

  • adds pay info. and methods, keeps other traits

 class Manager extends Employee

  • adds info. about employees managed, changes pay

mechanism, keeps other traits

slide-5
SLIDE 5

 class SavingsAccount extends BankAccount {

// added fields // added methods }

 Say “SavingsAccount is a BankAccount”  Superclass

rclass: BankAccount

 Subcl

clas ass: SavingsAccount

Q2

slide-6
SLIDE 6

The “superest” class in Java Still means “is a” Solid line shows inheritance

Q3

slide-7
SLIDE 7

 class ClickHandler implements MouseListener

  • ClickHandler pro

romises mises to implement all the methods of MouseListener

 class CheckingAccount extends BankAccount

  • CheckingAccount inher

herits its (or overrides) all the methods of BankAccount

For cl client ent code reuse For implemen lementation ation code reuse

slide-8
SLIDE 8
slide-9
SLIDE 9

 Inher

herit methods unchanged

 Ove

verride rride methods

  • Declare a new method with same signature to use

instead stead of superclass method

 Ad

Add entirely new methods not in superclass

Q4

slide-10
SLIDE 10

 ALWA

WAYS YS inherit erit all fields unchanged

 Can add entirely new fields not in superclass

DANGER! Don’t use the same name as a superclass field!

Q5

slide-11
SLIDE 11

 Calling superclass method

hod:

  • super.methodName(args);

 Calling superclass construct

structor

  • r:
  • super(args);

Must be the first line of the subclass constructor

Q6

slide-12
SLIDE 12

 A subclass instance is a superclass instance

  • Polymorphism still works!
  • BankAccount ba = new SavingsAccount();

ba.deposit(100);

 But not the other way around!

  • SavingsAccount sa = new BankAccount();

sa.addInterest();

 Why not?

BOOM!

For cl client ent code reuse

Q7

slide-13
SLIDE 13

 Can use:

  • public void transfer(double amt, BankAccount o){

withdraw(amount);

  • .deposit(amount);

}

in BankAccount

 To transfer between different accounts:

  • SavingsAccount sa = …;
  • CheckingAccount ca = …;
  • sa.transfer(100, ca);
slide-14
SLIDE 14

 Hybrid of superclasses and interfaces

  • Like regular superclass:

 Provide implementation of some methods

  • Like interfaces

 Just provide signatures and docs of other methods  Can’t be instantiated

 Example:

  • public abstract class BankAccount {

/** documentation here */ public abstract void deductFees(); … }

Elided methods as before

slide-15
SLIDE 15

 Review

  • public—any code can see it
  • private—only the class itself can see it

 Others

  • defaul

ault (i.e., no modifier)—only code in the same package kage can see it

 good choice for classes

  • protected—like default, but

subclasses also have access

 sometimes useful for helper methods

Bad for fields!

Q8

slide-16
SLIDE 16

Linear Lights Out

Q9-Q10

slide-17
SLIDE 17

Demo UML Design Questions