View
229
Download
4
Category
Tags:
Preview:
Citation preview
School of Computer Science & Information Technology
School of Computer Science & Information Technology
G6DICP - Lecture 16G6DICP - Lecture 16
InheritanceInheritance
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
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)
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
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
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!
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
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
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”
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{{
……}}
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.
Recommended