Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.

Similar presentations


Presentation on theme: "Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting."— Presentation transcript:

1 Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting A great deception

2 Lecture 13 It runs in the family.. OO

3 Coming up Inheritance –C–Code duplication –S–Super classes –C–Constructors Polymorphic collections –“–“Anywhere a super class is, a sub class can go” Casting A great deception

4 What do we mean by inheritance? Inheritance is something you get from your parents: – Appearance – Behaviour traits – Possessions OO

5 Inheritance happens in coding too The following example is based on the the DOME project from the BlueJ book DOME :Database of Multimedia equipment OO

6 Common properties Think about music CDs and films on DVDs... What properties do they share?

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling DoME objects

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling DoME classes top half shows fields bottom half shows methods

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling DoME object model

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling CD source code public class CD{ private String title; private String artist; private String comment; CD(String theTitle, String theArtist) { title = theTitle;artist = theArtist;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) [ ]

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling DVD source code public class DVD{ private String title; private String director; private String comment; DVD(String theTitle, String theDirector) { title = theTitle;director = theDirector;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...} incomplete (comments!) [ ]

12 There is a lot of saying the same thing.. public class CD{ private String title; private String artist; private String comment; CD(String theTitle, String theArtist) { title = theTitle;artist = theArtist;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...}public class DVD{ private String title; private String director; private String comment; DVD(String theTitle, String theDirector) { title = theTitle;director = theDirector;comment = " "; } void setComment(String newComment) {... } String getComment() {... } void print() {... }...}

13 Code Duplication Code duplication is a Bad Thing Duplicated code makes for lengthy, heavy programs It makes development error prone It makes maintenance hard If you debug one bit of code, you have to change each duplicated code fragment individually. This can take ages! If you have 150 classes with the same bit of code, it’s very easy to miss one... OO

14 Wouldn’t it be good......if a class could inherit some properties that it shares with other classes...... a bit like a common template......shipping out common functionality and keeping the specific stuff in the class... In Object Orientated programming, this is known as inheritance OO

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using inheritance

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Inheritance hierarchies

17 Making super and sub So how do I tell something to inherit?

18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Inheritance in Java public class Item {... } public class CD extends Item {... } public class DVD extends Item {... } no change here change here

19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Superclass public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; // constructors and methods omitted. }

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Subclasses public class CD extends Item { private String artist; private int numberOfTracks; // constructors and methods omitted. } public class DVD extends Item { private String director; // constructors and methods omitted. }

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; /** * Initialise the fields of the item. */ public Item(String theTitle, int time) { title = theTitle; playingTime = time; gotIt = false; comment = ""; } // methods omitted } Inheritance and constructors This is the super class constructor

22 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Inheritance and constructors public class CD extends Item { private String artist; private int numberOfTracks; /** * Constructor for objects of class CD */ public CD(String theTitle, String theArtist, int tracks, int time) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } // methods omitted } This is how you call a constructor from a super class

23 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Superclass constructor call Subclass constructors must always contain a 'super' call. If none is written, the compiler inserts one (without parameters) – works only, if the superclass has a constructor without parameters Must be the first statement in the subclass constructor.

24 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Adding more item types

25 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Deeper hierarchies

26 Coming up Inheritance –C–Code duplication –S–Super classes –C–Constructors Polymorphic collections –“–“Anywhere a super class is, a sub class can go” Casting A great deception

27 Collections Using collections and inheritance OO

28 Animal hierarchy AnimalCanineDogWolfFelineCatTigerRodentHamster OO

29 Pet Shop Let’s say we want to use an ArrayList to model the collection of animals that people can adopt as pets Do we need a separate: – ArrayList – ArrayList ? OO

30 Substitution It would be better if we could have: –A–ArrayList<Animal> which would take them all We can! Anywhere a super class is, a sub class can go. This is called substitution OO

31 Where it works Substitution works in many situations, not just collections: Canine c = new Dog(); public Feline putFoodOut(){ Cat moggy = new Cat(); return moggy; } OO

32 But.. It only works one way: – Feline mouser = new Cat(); – Cat mouser = new Feline(); A Cat is a Feline but a Feline is not necessarily a Cat OO

33 Coming up Inheritance –C–Code duplication –S–Super classes –C–Constructors Polymorphic collections –“–“Anywhere a super class is, a sub class can go” Casting A great deception

34 Casting What about taking an Animal out of the pet shop ArrayList? //petshop is an ArrayList Cat myPetCat; myPetCat = petshop.get(1); The compiler does type checking – it doesn’t know that what you are taking out is actually a Cat Won’t compile! OO

35 Casting How can we tell the complier about type? Cat myPetCat; myPetCat = petshop.get(1); This is called casting It only works if the cast is correct! You can’t suddenly change a dog into a cat! You can cast primitives too (Cat) OO

36 Coming up Inheritance –C–Code duplication –S–Super classes –C–Constructors Polymorphic collections –“–“Anywhere a super class is, a sub class can go” Casting A great deception

37 A great deception System.out.println(thisObject); When some objects are called you get the address Other times, you get something sensible like – printing a String – printing an Integer – etc Why?

38 And for that matter... What about the ‘default constructor’ – Where does that come from? How about the.equals() method, – it’s not a part of the string class So where do these things come from?

39 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Object class All classes inherit from Object.

40 A great discovery You’ve been dabbling with inheritance the whole time You don’t need to extend the Object class (you can if you really want to) as every object extends the Object class Look it up on the API


Download ppt "Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting."

Similar presentations


Ads by Google