object-oriented programming inheritance · 2010. 2. 22. · object-oriented programming inheritance...

32
. . . . . . . . . Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 1 / 30

Upload: others

Post on 16-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

.

.

. ..

.

.

Object-Oriented ProgrammingInheritance

Ewan Klein

School of Informatics

Inf1 :: 2009/10

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 1 / 30

Page 2: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Admin

Coming attractions

Week 7: More revision sessions at 1500 and 1600 this coming Friday (AT5.0)

Week 8: Revision sessions can be scheduled if there’s a demand

Week 9: Mock Exam; no tutorials, no labs, (yes lecture)

Week 10: OOP Competition (interactive computer game), judging onFriday 19th March

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 2 / 30

Page 3: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Classes with Stuff in Common

takeExam()graduate()party()

matricNonameagemailBox

UG Student PG Student

takeExam()graduate()party()tutor()

matricNonameagemailBox

UML diagram: class name, instance variables, methods.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 3 / 30

Page 4: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Classes with Stuff in Common

takeExam()graduate()party()

matricNonameagemailBox

UG Student PG Student

takeExam()graduate()party()tutor()

matricNonameagemailBox

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 4 / 30

Page 5: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Abstracting Common Stuff

graduate()//get BSc

UG PGgraduate()//get PhDtutor()

takeExam()party()

matricNonameagemailBox

Student

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 5 / 30

Page 6: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Abstracting Common Stuff

Subclass (e.g. UG) inherits from superclass (e.g. Student).

In Java: subclass extends superclass.

So subclass inherits all the members — instance variables and methods — ofsuperclass.

A subclass can add new members of its own

By default, methods that are inherited from superclass have sameimplementation in subclass; except

if subclass overrides the inherited methods.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 6 / 30

Page 7: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Example, 1

+treatPatient(): void+makeIncisions(): void

Surgeon

+giveAdvice(): void

FamilyDoctor

+makesHouseCalls

+treatPatient(): void

+worksAtHospital

Doctor

generalization

overriding method

For handy guide to UML, seehttp://www.loufranco.com/blog/assets/cheatsheet.pdf

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 7 / 30

Page 8: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Example, 2

.Doctor

..

.

. ..

.

.

public class Doctor {public boolean worksAtHospital;public void treatPatient() {

// perform a checkup}

}

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 8 / 30

Page 9: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Example, 3

.Surgeon..

.

. ..

.

.

public class Surgeon extends Doctor {public void treatPatient() {

// perform surgery// overrides inherited method

}public void makeIncisions() {

// use a scalpel}

}

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

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 9 / 30

Page 10: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Example, 4

.FamilyDoctor..

.

. ..

.

.

public class FamilyDoctor extends Doctor {public boolean makesHouseCalls;public void giveAdvice() {

// tells you to wrap up warmly}

}

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 10 / 30

Page 11: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Method Overriding

Method m in subclass B overrides method m’ in super class A if m has exactlythe same signature (i.e. name and parameters) and return type as m’.

In effect, m replaces the implementation of m’.

If s is an object of class Surgeon, then calling s.treatPatient() uses thesubclass implementation of treatPatient().

NB The story about return types is slightly more complicated.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 11 / 30

Page 12: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Type Hierarchy View in Eclipse

Hierarchy Tree Pane

Members Pane

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 12 / 30

Page 13: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Subclasses

Right-click on a class name and select Open Type Hierarchy

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 13 / 30

Page 14: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

FamilyDoctor Members

Inherited and non-inherited members

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 14 / 30

Page 15: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

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 thatparticular subclass type.

...4 Carry out further abstraction by looking for groups of subclasses that mighthave common behaviours.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 15 / 30

Page 16: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

X IS-A Y?

ClassX extends ClassYCan we say that ClassX IS-A ClassY?

That is, an instance of ClassX can do anything (and maybe more) that aninstance of ClassY can do?

.IS-A Candidates..

.

. ..

.

.

...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 (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 16 / 30

Page 17: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Doctor Example in Eclipse

Where does Doctor belong in the class hierarchy?

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 17 / 30

Page 18: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

The class Object

.Doctor’s Superclass

..

.

. ..

.

.

public class Doctor {

extends Object {

boolean worksAtHospital;void treatPatient() {

// perform a checkup}

}

Object is the superclass of every class in Java!

If a class doesn’t explicitly extend some superclass, then it implicitly extendsObject.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 18 / 30

Page 19: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

The class Object

.Doctor’s Superclass

..

.

. ..

.

.

public class Doctor

{

extends Object {boolean worksAtHospital;void treatPatient() {

// perform a checkup}

}

Object is the superclass of every class in Java!

If a class doesn’t explicitly extend some superclass, then it implicitly extendsObject.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 18 / 30

Page 20: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Object as a Polymorphic Type

.Javadoc for ArrayList’s contains() method..

.

. ..

.

.

public boolean contains(Object elem)Returns true if this list contains the specified element.

Parameters: elem - element whose presence in this List is to be tested.

Returns: true if the specified element is present; false otherwise.

Since every class in Java is a subclass of Object, the contains() method cantake any object as an argument.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 19 / 30

Page 21: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

FamilyDoctor Members

Inherited and non-inherited members

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 20 / 30

Page 22: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Some Methods of Object

Object defines methods that are available to every class. E.g.,

equals(Object o) — test whether two objects are equal.

hashCode() — numerical ID; equal objects must have equal hash codes.

toString() — returns a textual representation of an object; automaticallyinvoked by methods like System.out.println().

Sometimes the default implementation of toString() is not what we want, souseful to override it.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 21 / 30

Page 23: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Overriding toString(), 1

Create an arbitrary (empty) class:.Number Two..

.

. ..

.

.

public class NumberTwo {}

What happens if we supply a NumberTwo object n2 as the argument ofSystem.out.print()?

Invokes n2.toString(), which is inherited from Object.

.Output.... ..

.

.NumberTwo@10d448

Prints the class Name, @, and the object’s hashcode concatenated into a string.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 22 / 30

Page 24: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Overriding toString(), 1

Create an arbitrary (empty) class:.Number Two..

.

. ..

.

.

public class NumberTwo {}

What happens if we supply a NumberTwo object n2 as the argument ofSystem.out.print()?

Invokes n2.toString(), which is inherited from Object.

.Output.... ..

.

.NumberTwo@10d448

Prints the class Name, @, and the object’s hashcode concatenated into a string.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 22 / 30

Page 25: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

The Prisoner (1967): I am not a number!

Patrick McGoohan

http://www.youtube.com/watch?v=29JewlGsYxs

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 23 / 30

Page 26: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Overriding toString(), 2

Let’s override the inherited toString() method.Number Six..

.

. ..

.

.

public class NumberSix {public String toString() {

return ”I am not a number – I am a free man!”;}

}

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 24 / 30

Page 27: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Overriding toString(), 3

.PrisonerLauncher

..

.

. ..

.

.

public class PrisonerLauncher {public static void main(String[] args) {

NumberTwo n2 = new NumberTwo();NumberSix n6 = new NumberSix();System.out.println(”You are Number Two: ” + n2);System.out.println(”You are Number Six: ” + n6);

}}

.Output..

.

. ..

.

.

You are Number Two: NumberTwo@10d448You are Number Six: I am not a number – I am a free man!

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 25 / 30

Page 28: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Printing out Arrays.PrintArray..

.

. ..

.

.

import java.util.ArrayList;import java.util.Arrays;public class PrintArray {

public static void main(String[] args) {int [] numbers1 = { 33, 5, 22, 0, -3, 700 } ;ArrayList<Integer> numbers2 = new ArrayList<Integer>();for (int i : numbers1) {

numbers2.add(i+10);}

System.out.println(”Using the array:”̑ + numbers1);System.out.println(”Using Arrays.toString:”̑

+ Arrays.toString(numbers1));System.out.println(”Using the ArrayList:”̑ + numbers2);

}}

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 26 / 30

Page 29: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Printing out Arrays

.Output..

.

. ..

.

.

Using the array: [I@72e3b895Using Arrays.toString: [33, 5, 22, 0, -3, 700]Using the ArrayList: [43, 15, 32, 10, 7, 710]

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 27 / 30

Page 30: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Summary

Implementing an interface: the implementing class must provide bodies forall the methods declared in the interface.Inheriting from a superclass:

I the subclass gets all the members (instance variables and methods) of thesuperclass;

I the subclass may add members, and also override methods.I So subclass extends (adds to) the behaviour of its superclass.

Inheritance corresponds roughly to taxonomic relations for everyday concepts— but these intuitions not always relevant for software development.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 28 / 30

Page 31: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

When to use What

Use inheritance when you want the subclass to be able to use the methodimplementations of its superclass.

Use an interface when you want your classes to conform to an API, but eachclass has its own implementation of all the interface methods.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 29 / 30

Page 32: Object-Oriented Programming Inheritance · 2010. 2. 22. · Object-Oriented Programming Inheritance Ewan Klein School of Informatics Inf1 ::2009/10 Ewan Klein (School of Informatics)

. . . . . .

Reading, etc

Read Chapter 7 of Head First Java.

More on Inheritance next Monday.

Ewan Klein (School of Informatics) Object-Oriented ProgrammingInheritance Inf1 :: 2009/10 30 / 30