180 likes | 286 Views
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.
E N D
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
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
Hierarchies Thing Alive Mineral Vegetal Animal Mammals Monotremes Marsupials Placentals • Each level has • own defining features and • defining features from previous - inherited
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)
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
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
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
The “extends”reserved word Class modifier Declares one class to be a subclass of another For example: class Tiger extends Cat { … }// end class Tiger
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.
Superclass classThing { privateString mstrName; publicThing(String strName) { mstrName = strName; } // Constructor () publicString getName() { returnmstrName; } // getName() publicbooleanisLiving() { returnfalse; } // isLiving() }// end class Thing
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
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
classMammal extends Animal { privateintmiNumLegs; publicMammal(String strName, intiNumLegs) { super(strName); miNumLegs = iNumLegs; }// Constructor () publicString type() { return"Mammal"; } // type() publicintgetNumLegs() { returnmiNumLegs; }// getNumLegs() }// end class Mammal
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
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
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