Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "School of Computer Science & Information Technology G6DICP - Lecture 16 Inheritance."— Presentation transcript:

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

2 2 Definition of classes Classes may be defined in terms of other classes Classes may be defined in terms of other classes For example: For example: Tigers, cheetahs, leopards & jaguars are all types of cats Tigers, cheetahs, leopards & jaguars are all types of cats Class tiger is a subclass of class cat Class tiger is a subclass of class cat Ball point pens, fountain pens & marker pens are all types of pens Ball point pens, fountain pens & marker pens are all types of pens Ball point pen is a subclass of class pen Ball point pen is a subclass of class pen Subclasses inherit properties from their parent Subclasses inherit properties from their parent All cats are furry, and have big teeth - therefore tigers are furry and have big teeth 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 All pens contain ink - therefore marker pens contain ink

3 3 Class hierarchies Classes are arranged into hierarchies Classes are arranged into hierarchies Subclasses provide specialised behaviour, whereas superclasses are more general. Subclasses provide specialised behaviour, whereas superclasses are more general. Inheritance is one-way (ie downwards) Inheritance is one-way (ie downwards) All Java classes are ultimately inherited from class Object All Java classes are ultimately inherited from class Object Methods are inherited down a hierarchy Methods are inherited down a hierarchy They may be left unchanged They may be left unchanged They may be modified (ie overridden) They may be modified (ie overridden)

4 4 Inheritance - of properties Invertebrates Invertebrates Vertebrates Backbone Vertebrates Backbone Fish Scales Fish Scales Amphibians Amphibians Reptiles Reptiles Birds Feathers Birds Feathers Mammals Fur Mammals Fur Bats Wings Bats Wings Cattle Hooves Cattle Hooves Carnivores Big Teeth Carnivores Big Teeth Dogs Dogs Cats Cats Lion Mane Lion Mane Tiger Stripes Tiger Stripes Animals

5 5 Inheritance - of properties Invertebrates Invertebrates Vertebrates Backbone Vertebrates Backbone Fish Scales Fish Scales Amphibians Amphibians Reptiles Reptiles Birds Feathers Birds Feathers Mammals Fur Mammals Fur Bats Wings Bats Wings Cattle Hooves Cattle Hooves Carnivores Big Teeth Carnivores Big Teeth Dogs Dogs Cats Cats Lion Mane Lion Mane Tiger Stripes Tiger Stripes Animals Tigers are vertebrates - thus they have a backbone

6 6 Inheritance - of properties Invertebrates Invertebrates Vertebrates Backbone Vertebrates Backbone Fish Scales Fish Scales Amphibians Amphibians Reptiles Reptiles Birds Feathers Birds Feathers Mammals Fur Mammals Fur Bats Wings Bats Wings Cattle Hooves Cattle Hooves Carnivores Big Teeth Carnivores Big Teeth Dogs Dogs Cats Cats Lion Mane Lion Mane Tiger Stripes Tiger Stripes Animals Tigers are not birds - they do not have feathers!

7 7 Inheritance - of properties Invertebrates Invertebrates Vertebrates Backbone Vertebrates Backbone Fish Scales Fish Scales Amphibians Amphibians Reptiles Reptiles Birds Feathers Birds Feathers Mammals Fur Mammals Fur Bats Wings Bats Wings Cattle Hooves Cattle Hooves Carnivores Big Teeth Carnivores Big Teeth Dogs Dogs Cats Cats Lion Mane Lion Mane Tiger Stripes Tiger Stripes Animals Tigers are carnivores - they have big teeth

8 8 Inheritance of behaviour (methods) Pencil Method - sharpen Pencil Method - sharpen Pen Property - Ink Colour Pen Property - Ink Colour Ball-point pen Ball-point pen Fountain pen Method - fill with ink Fountain pen Method - fill with ink Felt-tip pen Method - remove cap Felt-tip pen Method - remove cap Permanent Marker pen Permanent Marker pen Dry Wipe pen Dry Wipe pen Writing Implements Method - Draw Line

9 9 Sub and Super Classes Pencil Pencil Pen Superclass Pen Superclass Ball-point pen Ball-point pen Fountain pen Fountain pen Felt-tip pen Class Felt-tip pen Class Permanent Marker pen Subclass Permanent Marker pen Subclass Dry Wipe pen Subclass Dry Wipe pen Subclass Writing Implements Ancestor Class Consider the following classes, relative to Felt-tip pen

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

11 11 The super reserved word The super reserved word refers to the immediate superclass of a class. The super reserved word refers to the immediate superclass of a class. The superclass constructor may be invoked by calling super. The superclass constructor may be invoked by calling super. On its own super invokes the constructor of the immediate superclass. On its own super invokes the constructor of the immediate superclass.


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

Similar presentations


Ads by Google