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

G6DICP - Lecture 16

Feb 23, 2016

Download

Documents

Elda

G6DICP - Lecture 16. Inheritance. Definition of classes. Classes may be defined in terms of other classes For example: Tigers, cheetahs, leopards & jaguars are all types of cats Class tiger is a subclass of class cat Ball point pens, fountain pens & marker pens are all types of pens - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: G6DICP - Lecture 16

School of Computer Science & Information Technology

G6DICP - Lecture 16G6DICP - Lecture 16

InheritanceInheritance

Page 2: G6DICP - Lecture 16

2

Definition 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: G6DICP - Lecture 16

3

Class 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: G6DICP - Lecture 16

4

Inheritance - 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: G6DICP - Lecture 16

5

Inheritance - 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

AnimalsTigers are

vertebrates - thus they have a backbone

Page 6: G6DICP - Lecture 16

6

Inheritance - 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: G6DICP - Lecture 16

7

Inheritance - 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: G6DICP - Lecture 16

8

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: G6DICP - Lecture 16

9

Sub 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: G6DICP - Lecture 16

10

The “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: G6DICP - Lecture 16

11

The “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.