Upload
hasad-beasley
View
19
Download
2
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
2Amadeo Ascó , Adam Moore
Previously
• Java String– String as an Object– String as an Array of Characters– Equalities– Length– Index– Substring– Split
3Amadeo Ascó , Adam Moore
Overview
• Defining Classes
• Inheritance of Properties
• Inheritance of Methods
• Sub and super classes
4Amadeo Ascó , Adam Moore
Defining 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
5Amadeo Ascó , Adam Moore
HierarchiesThing
Mineral Alive
Animal Vegetal
Mammals
Monotremes Marsupials Placentals
Each level has• own defining features and• defining features from previous - inherited
6Amadeo Ascó , Adam Moore
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)
7Amadeo Ascó , Adam Moore
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 vertebratesThus they have a backbone
Tigers are carnivoresThus they have big teeth
Tigers are not birdsThus they do not have feathers
8Amadeo Ascó , Adam Moore
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
9Amadeo Ascó , Adam Moore
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
10Amadeo Ascó , Adam Moore
The “extends”reserved word
• Class modifier • Declares one class to be a subclass of another • For example:
class Tiger extends Cat {
…} // end class Tiger
11Amadeo Ascó , Adam Moore
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.
12Amadeo Ascó , Adam Moore
class Thing {private String mstrName;
public Thing(String strName) {mstrName = strName;
} // Constructor ()
public String getName() {return mstrName;
} // getName()
public boolean isLiving() {return false;
} // isLiving()} // end class Thing
class Thing {private String mstrName;
public Thing(String strName) {mstrName = strName;
} // Constructor ()
public String getName() {return mstrName;
} // getName()
public boolean isLiving() {return false;
} // isLiving()} // end class Thing
Superclass
13Amadeo Ascó , Adam Moore
class Alive extends Thing {public Alive(String strName) {
super(strName);} // Constructor ()
public String group() {return "Alive";
} // group()
public boolean isLiving() {return true;
} // isLiving()
} // end class Alive
class Alive extends Thing {public Alive(String strName) {
super(strName);} // Constructor ()
public String group() {return "Alive";
} // group()
public boolean isLiving() {return true;
} // isLiving()
} // end class Alive
class Mineral extends Thing {public Mineral(String strName) {
super(strName);} // Constructor ()
public String group() {return "Mineral";
} // group()
} // end class Mineral
class Mineral extends Thing {public Mineral(String strName) {
super(strName);} // Constructor ()
public String group() {return "Mineral";
} // group()
} // end class Mineral
Subclasses
14Amadeo Ascó , Adam Moore
class Animal extends Alive {public Animal(String strName) {
super(strName);} // Constructor ()
public String subgroup() {return "Animal";
} // subgroup()
} // end class Animal
class Animal extends Alive {public Animal(String strName) {
super(strName);} // Constructor ()
public String subgroup() {return "Animal";
} // subgroup()
} // end class Animal
Subclasses
class Vegetal extends Alive {public Vegetal(String strName) {
super(strName);} // Constructor ()
public String subgroup() {return "Vegetal";
} // subgroup()
} // end class Vegetal
class Vegetal extends Alive {public Vegetal(String strName) {
super(strName);} // Constructor ()
public String subgroup() {return "Vegetal";
} // subgroup()
} // end class Vegetal
15Amadeo Ascó , Adam Moore
class Mammal extends Animal {private int miNumLegs;
public Mammal(String strName, int iNumLegs) {super(strName);
miNumLegs = iNumLegs;} // Constructor ()
public String type() {return "Mammal";
} // type()public int getNumLegs() {
return miNumLegs;} // getNumLegs()
} // end class Mammal
class Mammal extends Animal {private int miNumLegs;
public Mammal(String strName, int iNumLegs) {super(strName);
miNumLegs = iNumLegs;} // Constructor ()
public String type() {return "Mammal";
} // type()public int getNumLegs() {
return miNumLegs;} // getNumLegs()
} // end class Mammal
16Amadeo Ascó , Adam Moore
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());
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());
Fromclass
ThingAliveAliveAnimalMammalMammal
Fromclass
ThingAliveAliveAnimalMammalMammal
Name: ElephantIs living been? trueGroup: AliveSubgroup: AnimalType: MammalNum legs: 4
17Amadeo Ascó , Adam Moore
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
18Amadeo Ascó , Adam Moore
class Alive extends Thing {public Alive(String strName) {
super(strName);} // Constructor ()
public String group() {return "Alive";
} // group()
...} // end class Alive
class Alive extends Thing {public Alive(String strName) {
super(strName);} // Constructor ()
public String group() {return "Alive";
} // group()
...} // end class Alive
Sub and Super Classes
class Thing {private String mstrName;
public Thing(String strName) {mstrName = strName;
} // Constructor ()
public String getName() {return mstrName;
} // getName()...
} // end class Thing
class Thing {private String mstrName;
public Thing(String strName) {mstrName = strName;
} // Constructor ()
public String getName() {return mstrName;
} // getName()...
} // end class Thing
Example