Object-Oriented Programming Inheritance . . . . . Ewan Klein - - PowerPoint PPT Presentation

object oriented programming inheritance
SMART_READER_LITE
LIVE PREVIEW

Object-Oriented Programming Inheritance . . . . . Ewan Klein - - PowerPoint PPT Presentation

. . Object-Oriented Programming Inheritance . . . . . Ewan Klein Inf1 :: 2008/09 . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance . Inheritance and Polymorphism Chapters 7 9 of Head First Java . Central to whole


slide-1
SLIDE 1

. . . . . .

. . . . . . .

Object-Oriented Programming Inheritance

Ewan Klein Inf1 :: 2008/09

Ewan Klein Object-Oriented ProgrammingInheritance

slide-2
SLIDE 2

. . . . . .

. Inheritance and Polymorphism

Chapters 7 – 9 of Head First Java. Central to whole business of OOP. We’ll take it slowly.

Ewan Klein Object-Oriented ProgrammingInheritance

slide-3
SLIDE 3

. . . . . .

. Classes with Stuff in Common

takeExam() graduate() party() matricNo name age mailBox UG Student PG Student takeExam() graduate() party() tutor() matricNo name age mailBox

UML diagram: class name, instance variables, methods.

Ewan Klein Object-Oriented ProgrammingInheritance

slide-4
SLIDE 4

. . . . . .

. Abstracting Common Stuff

graduate() //get BSc UG PG graduate() //get PhD tutor() takeExam() party() matricNo name age mailBox Student

Ewan Klein Object-Oriented ProgrammingInheritance

slide-5
SLIDE 5

. . . . . .

. Abstracting Common Stuff

Subclass (e.g. UG) inherits from superclass (e.g. Student). In Java: subclass extends superclass. So subclass gets all the members — instance variables and methods — of superclass. Also:

subclass can add new members of its own; and

  • verride inherited methods.

Ewan Klein Object-Oriented ProgrammingInheritance

slide-6
SLIDE 6

. . . . . .

. Doctor Example, 1

treatPatient() makeIncisions() Surgeon giveAdvice() FamilyDoctor makesHouseCalls treatPatient() worksAtHospital Doctor

Ewan Klein Object-Oriented ProgrammingInheritance

slide-7
SLIDE 7

. . . . . .

. Doctor Example, 2

. Doctor . . . . . . . .

public class Doctor { boolean worksAtHospital; void treatPatient() { // perform a checkup } }

Ewan Klein Object-Oriented ProgrammingInheritance

slide-8
SLIDE 8

. . . . . .

. Doctor Example, 3

. Surgeon . . . . . . . .

public class Surgeon extends Doctor { void treatPatient() { // perform surgery } void makeIncisions() { // use a scalpel } }

NB We put this class into a new file Surgeon.java

Ewan Klein Object-Oriented ProgrammingInheritance

slide-9
SLIDE 9

. . . . . .

. Doctor Example, 4

. FamilyDoctor . . . . . . . .

public class FamilyDoctor extends Doctor { boolean makesHouseCalls; void giveAdvice() { // tells you to wrap up warmly } }

Ewan Klein Object-Oriented ProgrammingInheritance

slide-10
SLIDE 10

. . . . . .

. The Design Process

.

..

1

Look for objects that have common attributes and behaviours. .

..

2

Design a class that represents the common state and behaviour. .

..

3

Decide if a subclass needs method implementations that are specific to that particular subclass type. .

..

4

Carry out further abstraction by looking for groups of subclasses that might have common behaviours.

Ewan Klein Object-Oriented ProgrammingInheritance

slide-11
SLIDE 11

. . . . . .

. X IS-A Y?

ClassX extends ClassY

Can we say that ClassX IS-A ClassY? That is, an instance of ClassX can do anything (and maybe more) that an instance of ClassY can do? .

..

1

Kitchen extends Room

.

..

2

Room extends House

.

..

3

Violinist extends Musician

.

..

4

Sink extends Kitchen

.

..

5

Musician extends Person

.

..

6

Madonna extends Singer

.

..

7

Soprano extends Musician

Ewan Klein Object-Oriented ProgrammingInheritance

slide-12
SLIDE 12

. . . . . .

. Quiz!!

Short Quiz to do in class. Covers material in HFJ and lectures. To give me feedback on how you are doing.

Don’t need to put your name. Pleae leave completed sheets with me or at top of room before you depart.

Ewan Klein Object-Oriented ProgrammingInheritance

slide-13
SLIDE 13

. . . . . .

. Reading, etc

Read Chapter 7 of Head First Java. More on Inheritance on Friday. NB First OOP Lecture next week is Monday 16th February at 2.00 pm — swapped with D&A Slot.

Ewan Klein Object-Oriented ProgrammingInheritance