object oriented programming inheritance
play

Object-Oriented Programming Inheritance . . . . . Ewan Klein - PowerPoint PPT Presentation

. . Object-Oriented Programming Inheritance . . . . . Ewan Klein Inf1 :: 2008/09 . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance . Inheritance and Polymorphism Chapters 7 9 of Head First Java . Central to whole


  1. . . Object-Oriented Programming Inheritance . . . . . Ewan Klein Inf1 :: 2008/09 . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  2. . Inheritance and Polymorphism Chapters 7 – 9 of Head First Java . Central to whole business of OOP. We’ll take it slowly. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  3. . Classes with Stuff in Common PG Student UG Student matricNo matricNo name name age age mailBox mailBox takeExam() takeExam() graduate() graduate() party() party() tutor() UML diagram: class name, instance variables, methods. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  4. . Abstracting Common Stuff Student matricNo name age mailBox takeExam() party() UG PG graduate() graduate() //get BSc //get PhD tutor() . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  5. . Abstracting Common Stuff Subclass (e.g. UG ) inherits from superclass (e.g. Student ). In Java: subclass extends superclass. So subclass gets all the members — instance variables and methods — of superclass. Also: subclass can add new members of its own; and override inherited methods. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  6. . Doctor Example, 1 Doctor worksAtHospital treatPatient() Surgeon FamilyDoctor treatPatient() makesHouseCalls giveAdvice() makeIncisions() . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  7. . Doctor Example, 2 . Doctor . . . public class Doctor { boolean worksAtHospital; void treatPatient() { // perform a checkup } } . . . . . . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  8. . Doctor Example, 3 . Surgeon . . . public class Surgeon extends Doctor { void treatPatient() { // perform surgery } void makeIncisions() { // use a scalpel } } . . . . . NB We put this class into a new file Surgeon.java . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  9. . Doctor Example, 4 . FamilyDoctor . . . public class FamilyDoctor extends Doctor { boolean makesHouseCalls; void giveAdvice() { // tells you to wrap up warmly } } . . . . . . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  10. . The Design Process . . . Look for objects that have common attributes and behaviours. 1 . . . 2 Design a class that represents the common state and behaviour. . . . Decide if a subclass needs method implementations that are specific 3 to that particular subclass type. . . . Carry out further abstraction by looking for groups of subclasses that 4 might have common behaviours. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  11. . X IS-A Y ? ClassX extends ClassY Can we say that ClassX IS-A ClassY? That is, an instance of ClassX can do anything (and maybe more) that an instance of ClassY can do? . . . Kitchen extends Room 1 . . . Room extends House 2 . . . Violinist extends Musician 3 . . . Sink extends Kitchen 4 . . . Musician extends Person 5 . . . Madonna extends Singer 6 . . . Soprano extends Musician 7 . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  12. . Quiz!! Short Quiz to do in class. Covers material in HFJ and lectures. To give me feedback on how you are doing. Don’t need to put your name. Pleae leave completed sheets with me or at top of room before you depart. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

  13. . Reading, etc Read Chapter 7 of Head First Java . More on Inheritance on Friday. NB First OOP Lecture next week is Monday 16th February at 2.00 pm — swapped with D&A Slot. . . . . . . Ewan Klein Object-Oriented ProgrammingInheritance

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend