School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

11
chool of Computer Science & Information Technology G6DICP - Lecture 16 G6DICP - Lecture 16 Inheritance Inheritance

Transcript of School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

Page 1: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

School of Computer Science & Information Technology

School of Computer Science & Information Technology

G6DICP - Lecture 16G6DICP - Lecture 16

InheritanceInheritance

Page 2: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

2

Definition of classesDefinition of classes

Classes may be defined in terms of other classesClasses may be defined in terms of other classes For example:For example:

Tigers, cheetahs, leopards & jaguars are all types of catsTigers, cheetahs, leopards & jaguars are all types of cats Class tiger is a subclass of class catClass tiger is a subclass of class cat

Ball point pens, fountain pens & marker pens are all types Ball point pens, fountain pens & marker pens are all types of pensof pens

Ball point pen is a subclass of class penBall point pen is a subclass of class pen

Subclasses inherit properties from their parentSubclasses inherit properties from their parent All cats are furry, and have big teeth - therefore tigers are All cats are furry, and have big teeth - therefore tigers are

furry and have big teethfurry and have big teeth All pens contain ink - therefore marker pens contain inkAll pens contain ink - therefore marker pens contain ink

Page 3: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

3

Class hierarchiesClass hierarchies

Classes are arranged into hierarchiesClasses are arranged into hierarchies Subclasses provide specialised behaviour, whereas Subclasses provide specialised behaviour, whereas

superclasses are more general.superclasses are more general. Inheritance is one-way (ie downwards)Inheritance is one-way (ie downwards) All Java classes are ultimately inherited from class All Java classes are ultimately inherited from class

ObjectObject Methods are inherited down a hierarchyMethods are inherited down a hierarchy

They may be left unchangedThey may be left unchanged They may be modified (ie overridden)They may be modified (ie overridden)

Page 4: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

4

Inheritance - of propertiesInheritance - of properties

InvertebratesInvertebrates Vertebrates Vertebrates Backbone Backbone

Fish Fish Scales Scales

AmphibiansAmphibians ReptilesReptiles Birds Birds Feathers Feathers

Mammals Mammals Fur Fur

Bats Bats Wings Wings

Cattle Cattle Hooves Hooves

Carnivores Carnivores Big Teeth Big Teeth

DogsDogs CatsCats

Lion Lion Mane Mane Tiger Tiger Stripes Stripes

Animals

Page 5: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

5

Inheritance - of propertiesInheritance - of properties

InvertebratesInvertebrates Vertebrates Vertebrates Backbone Backbone

Fish Fish Scales Scales

AmphibiansAmphibians ReptilesReptiles Birds Birds Feathers Feathers

Mammals Mammals Fur Fur

Bats Bats Wings Wings

Cattle Cattle Hooves Hooves

Carnivores Carnivores Big Teeth Big Teeth

DogsDogs CatsCats

Lion Lion Mane Mane Tiger Tiger Stripes Stripes

Animals

Tigers are vertebrates - thus

they have a backbone

Page 6: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

6

Inheritance - of propertiesInheritance - of properties

InvertebratesInvertebrates Vertebrates Vertebrates Backbone Backbone

Fish Fish Scales Scales

AmphibiansAmphibians ReptilesReptiles Birds Birds Feathers Feathers

Mammals Mammals Fur Fur

Bats Bats Wings Wings

Cattle Cattle Hooves Hooves

Carnivores Carnivores Big Teeth Big Teeth

DogsDogs CatsCats

Lion Lion Mane Mane Tiger Tiger Stripes Stripes

Animals

Tigers are not birds -

they do not have feathers!

Page 7: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

7

Inheritance - of propertiesInheritance - of properties

InvertebratesInvertebrates Vertebrates Vertebrates Backbone Backbone

Fish Fish Scales Scales

AmphibiansAmphibians ReptilesReptiles Birds Birds Feathers Feathers

Mammals Mammals Fur Fur

Bats Bats Wings Wings

Cattle Cattle Hooves Hooves

Carnivores Carnivores Big Teeth Big Teeth

DogsDogs CatsCats

Lion Lion Mane Mane Tiger Tiger Stripes Stripes

Animals

Tigers are carnivores -

they have big teeth

Page 8: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

8

Inheritance of behaviour (methods)Inheritance of behaviour (methods)

Pencil Pencil Method - sharpenMethod - sharpen

Pen Pen Property - Ink ColourProperty - Ink Colour

Ball-point penBall-point pen Fountain pen Fountain pen Method - fill with inkMethod - fill with ink

Felt-tip pen Felt-tip pen Method - remove capMethod - remove cap

Permanent Marker penPermanent Marker pen Dry Wipe penDry Wipe pen

Writing Implements Method - Draw Line

Page 9: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

9

Sub and Super ClassesSub and Super Classes

PencilPencil Pen Pen SuperclassSuperclass

Ball-point penBall-point pen Fountain penFountain pen Felt-tip pen Felt-tip pen ClassClass

Permanent Marker pen Permanent Marker pen SubclassSubclass

Dry Wipe pen Dry Wipe pen SubclassSubclass

Writing Implements Ancestor Class

Consider the following classes, relative to “Felt-tip pen”

Page 10: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

10

The “extends” reserved wordThe “extends” reserved word Class modifierClass modifier Declares one class to be a subclass of anotherDeclares one class to be a subclass of another For example:For example:

class Tiger extends Catclass Tiger extends Cat{{

……}}

Page 11: School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance.

11

The “super” reserved wordThe “super” reserved word The “super” reserved word refers to the The “super” reserved word refers to the

immediate superclass of a class.immediate superclass of a class.

The superclass constructor may be invoked by The superclass constructor may be invoked by calling super.calling super.

On it’s own super invokes the constructor of the On it’s own super invokes the constructor of the immediate superclass.immediate superclass.