Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming

2 SE15: Inheritance32–2 Today’s Learning Objectives For you to appreciate how some classes are specialisations of other classes For you to understand the difference between composition and specialisation For you to see how specialisation is realised in Java through the mechanism of inheritance

3 SE15: Inheritance32–3 Lecture Outline Composition vs. specialisation Specialisation examples Inheritance in Java Basic syntax Use of super protected access modifier

4 SE15: Inheritance32–4 Object & Class Relationships Composition (‘Has’) Specialisation (‘Is a kind of’)

5 SE15: Inheritance32–5 Composition HandOfCardsCard “A hand has cards” “A hand is composed of cards”

6 SE15: Inheritance32–6 Specialisation & Inheritance Entities in the world are regarded as occupying particular positions in a hierarchy of categories or classes Classes near top are generic Classes lower down are more specialised ‘Is a kind of’ relationship exists between classes on different levels Owl is a kind of Bird Bird is a kind of Animal Specialisation relationship is expressed in programming languages through inheritance

7 SE15: Inheritance32–7 Specialisation Example Superclass (or ‘base class’) Subclass (or ‘derived class’)

8 SE15: Inheritance32–8 Another Example Person Name Gender Date of birth Student Place of study Programme of study Duration of study All Student instances will have these attributes… …plus these attributes that are specific to students Arrow must look exactly like this!

9 SE15: Inheritance32–9 Java Syntax for Inheritance public class SubclassName extends SuperclassName { Subclass instance variables Subclass methods } public class Student extends Person {... }

10 SE15: Inheritance32–10 Questions How do we initialise the bits of a Student that have been inherited from Person? How do we change the behaviour of toString so that the full details of a Student are included?

11 SE15: Inheritance32–11 Points to Note Private instance variables defined in the superclass are not accessible directly in the subclass Superclass can provide getter and setter methods Inherited instance variables can also be initialised by invoking superclass constructor using super Subclass inherits methods from the superclass and can override these to change behaviour if necessary Example: toString method

12 SE15: Inheritance32–12 super A way of referring to superclass from within subclass Used to invoke a superclass constructor from within subclass constructor super() will invoke default constructor of superclass Compiler will insert a super() for you automatically, to ensure initialisation of inherited parts

13 SE15: Inheritance32–13 Another Example HandOfCards sort() DeckOfCards shuffle() PokerHand isFlush(): boolean CardCollection add(Card) remove(): Card

14 SE15: Inheritance32–14 Protected Access Provides an alternative to using public getter and setter methods from superclass Protected instance variables are Accessible to subclasses Inaccessible to all other classes Useful analogy: family secrets

15 SE15: Inheritance32–15 Summary We have Seen that relationships can exist between classes Noted that one class can specialise (‘be a kind of’) another Looked at how Java implemented specialisation through the mechanism of inheritance Seen that private parts of superclasses are inherited by subclasses but are inaccessible Stressed the importance of initialising inherited instance variables properly, e.g., by using super Discussed the immediate level of access provided by the protected access modifier

16 SE15: Inheritance32–16 Follow-up Work Savitch, Chapter 7


Download ppt "Lecture 32 Inheritance COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google