110 likes | 255 Views
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
E N D
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 • Ball point pen is a subclass of class pen • Subclasses inherit properties from their parent • All cats are furry, and have big teeth - therefore tigers are furry and have big teeth • All pens contain ink - therefore marker pens contain ink
Class hierarchies • Classes are arranged into hierarchies • Subclasses provide specialised behaviour, whereas superclasses are more general. • Inheritance is one-way (ie downwards) • All Java classes are ultimately inherited from class Object • Methods are inherited down a hierarchy • They may be left unchanged • They may be modified (ie overridden)
Animals Inheritance - of properties • Invertebrates • Vertebrates Backbone • Fish Scales • Amphibians • Reptiles • Birds Feathers • Mammals Fur • Bats Wings • Cattle Hooves • Carnivores Big Teeth • Dogs • Cats • Lion Mane • Tiger Stripes
Animals Tigers are vertebrates - thus they have a backbone Inheritance - of properties • Invertebrates • Vertebrates Backbone • Fish Scales • Amphibians • Reptiles • Birds Feathers • Mammals Fur • Bats Wings • Cattle Hooves • Carnivores Big Teeth • Dogs • Cats • Lion Mane • Tiger Stripes
Animals Tigers are not birds - they do not have feathers! Inheritance - of properties • Invertebrates • Vertebrates Backbone • Fish Scales • Amphibians • Reptiles • Birds Feathers • Mammals Fur • Bats Wings • Cattle Hooves • Carnivores Big Teeth • Dogs • Cats • Lion Mane • Tiger Stripes
Animals Tigers are carnivores - they have big teeth Inheritance - of properties • Invertebrates • Vertebrates Backbone • Fish Scales • Amphibians • Reptiles • Birds Feathers • Mammals Fur • Bats Wings • Cattle Hooves • Carnivores Big Teeth • Dogs • Cats • Lion Mane • Tiger Stripes
Writing Implements Method - Draw Line Inheritance of behaviour (methods) • Pencil Method - sharpen • Pen Property - Ink Colour • Ball-point pen • Fountain pen Method - fill with ink • Felt-tip pen Method - remove cap • Permanent Marker pen • Dry Wipe pen
Writing Implements Ancestor Class Sub and Super Classes • Pencil • Pen Superclass • Ball-point pen • Fountain pen • Felt-tip pen Class • Permanent Marker pen Subclass • Dry Wipe pen Subclass Consider the following classes, relative to “Felt-tip pen”
The “extends” reserved word • Class modifier • Declares one class to be a subclass of another • For example: class Tiger extends Cat { … }
The “super” reserved word • The “super” reserved word refers to the immediate superclass of a class. • The superclass constructor may be invoked by calling super. • On it’s own super invokes the constructor of the immediate superclass.