Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 17 Inheritance.

Similar presentations


Presentation on theme: "1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 17 Inheritance."— Presentation transcript:

1 1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 17 Inheritance

2 2 Amadeo Ascó, Adam Moore Previously Java String – String as an Object – String as an Array of Characters – Equalities – Length – Index – Substring – Split

3 3 Amadeo Ascó, Adam Moore Overview Defining Classes Inheritance of Properties Inheritance of Methods Sub and super classes

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

5 5 Amadeo Ascó, Adam Moore Hierarchies Thing Mineral Alive Animal Vegetal Mammals MonotremesMarsupialsPlacentals Each level has own defining features and defining features from previous - inherited

6 6 Amadeo 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)

7 7 Amadeo 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 vertebrates Thus they have a backbone Tigers are carnivores Thus they have big teeth Tigers are not birds Thus they do not have feathers

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

9 9 Amadeo 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

10 10 Amadeo 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

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

12 12 Amadeo 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

13 13 Amadeo 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

14 14 Amadeo 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

15 15 Amadeo 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

16 16 Amadeo 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()); From class Thing Alive Animal Mammal From class Thing Alive Animal Mammal Name: Elephant Is living been? true Group: Alive Subgroup: Animal Type: Mammal Num legs: 4

17 17 Amadeo 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

18 18 Amadeo 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


Download ppt "1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 17 Inheritance."

Similar presentations


Ads by Google