1 / 18

Inheritance

17. Inheritance. Java String String as an Object String as an Array of Characters Equalities Length Index Substring Split. Previously. Overview. Defining Classes Inheritance of Properties Inheritance of Methods Sub and super classes.

Télécharger la présentation

Inheritance

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

  2. Java String String as an Object String as an Array of Characters Equalities Length Index Substring Split Previously

  3. Overview Defining Classes Inheritance of Properties Inheritance of Methods Sub and super classes

  4. 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 four feet - therefore tigers are furry and have four feet All pens contain ink - therefore ball point pens contain ink Defining Classes

  5. Hierarchies Thing Alive Mineral Vegetal Animal Mammals Monotremes Marsupials Placentals • Each level has • own defining features and • defining features from previous - inherited

  6. Class hierarchies Classes are arranged into hierarchies Subclasses provide specialised behaviour, whereas superclasses are more general. Inheritance is one-way (i.e. 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 (i.e. overridden)

  7. Inheritance - of properties Animals Invertebrates Vertebrates  Backbone Fish  Scales Amphibians Reptiles Birds  Feathers Mammals  Females with mammary glands Bats  Wings Cattle  Hooves Carnivore  Big Teeth Dogs Cats Tigers are vertebrates Thus they have a backbone Tigers are not birds Thus they do not have feathers Tigers are carnivores Thus they have big teeth

  8. Inheritance of behaviour (methods) Writing Implements  Method:Draw Line 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. Sub and Super Classes Consider the following classes, relative to “Felt-tip pen” Writing Implements Ancestor Class Pencil Pen Superclass Ball-point pen Fountain pen Felt-tip pen Class Permanent Marker pen Subclass Dry Wipe pen Subclass

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

  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 its own super invokes the constructor of the immediate superclass.

  12. Superclass classThing { privateString mstrName; publicThing(String strName) { mstrName = strName; } // Constructor () publicString getName() { returnmstrName; } // getName() publicbooleanisLiving() { returnfalse; } // isLiving() }// end class Thing

  13. Subclasses classMineral extends Thing { publicMineral(String strName) { super(strName); }// Constructor () publicString group() { return"Mineral"; }// group() }// end class Mineral classAlive extends Thing { publicAlive(String strName) { super(strName); }// Constructor () publicString group() { return"Alive"; }// group() publicbooleanisLiving() { returntrue; }// isLiving() }// end class Alive

  14. Subclasses classVegetal extends Alive { publicVegetal(String strName) { super(strName); }// Constructor () publicString subgroup() { return"Vegetal"; }// subgroup() }// end class Vegetal classAnimal extends Alive { publicAnimal(String strName) { super(strName); }// Constructor () publicString subgroup() { return"Animal"; }// subgroup() }// end class Animal

  15. classMammal extends Animal { privateintmiNumLegs; publicMammal(String strName, intiNumLegs) { super(strName); miNumLegs = iNumLegs; }// Constructor () publicString type() { return"Mammal"; } // type() publicintgetNumLegs() { returnmiNumLegs; }// getNumLegs() }// end class Mammal

  16. Mammal elephant = new Mammal("Elephant", 4); System.out.println("Name: " + elephant.getName()); System.out.println("Is living been? " + elephant.isLiving()); System.out.println("Group: " + elephant.group()); System.out.println("Subgroup: " + elephant.subgroup()); System.out.println("Type: " + elephant.type()); System.out.println("Num legs: " + elephant.getNumLegs()); From class Thing Alive Alive Animal Mammal Mammal Name: Elephant Is living been? true Group: Alive Subgroup: Animal Type: Mammal Num legs: 4

  17. Sub and Super Classes • In Java if no constructor has been defined • Java defines a default constructor for the class – Constructor without parameters • All subclass constructors call their previous class constructor • If parent class does not have defined implicitly a constructor then the default one is called • Otherwise you must call the parents constructor in your constructor

  18. Sub and Super Classes Example classThing { privateString mstrName; publicThing(String strName) { mstrName = strName; }// Constructor () publicString getName() { returnmstrName; }// getName() ... }// end class Thing classAlive extendsThing { publicAlive(String strName) { super(strName); }// Constructor () publicString group() { return"Alive"; }// group() ... }// end class Alive

More Related