1 / 11

G6DICP - Lecture 16

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

elda
Télécharger la présentation

G6DICP - Lecture 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. G6DICP - Lecture 16 Inheritance

  2. 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

  3. 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)

  4. 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

  5. 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

  6. 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

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

  8. 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

  9. 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”

  10. The “extends” reserved word • Class modifier • Declares one class to be a subclass of another • For example: class Tiger extends Cat { … }

  11. 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.

More Related