Object Oriented Programming in A Level

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Python Objects and Classes
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Introduction To System Analysis and design
Object Oriented Software Development
BCS 2143 Introduction to Object Oriented and Software Development.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
7-1 © Prentice Hall, 2007 Week 5: Conceptual Data Modeling Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
CS212: Object Oriented Analysis and Design Lecture 13: Relationship between Classes.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Object Oriented Software Development
M1G Introduction to Programming 2 5. Completing the program.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 12 Object-oriented design for more than one class.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
CSCE 240 – Intro to Software Engineering Lecture 3.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
The Movement To Objects
Chapter 5: Structural Modeling
Objects as a programming concept
MPCS – Advanced java Programming
Interface, Subclass, and Abstract Class Review
Final and Abstract Classes
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Introduction to Programmng in Python
An Introduction to Inheritance
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object Oriented Programming in Python
Object-oriented programming
Object Oriented Programming
Object-oriented programming
To Get Started Paper sheet
Object-oriented programming
Object-Oriented Programming
Inheritance Basics Programming with Inheritance
Teach A-Level Computer Science: Object-Oriented Programming in Python
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Java Inheritance.
Object Oriented Programming
Workshop for Programming And Systems Management Teachers
Inheritance and Polymorphism
Object Oriented Programming in A Level
Review of Previous Lesson
C++ Programming CLASS This pointer Static Class Friend Class
Final and Abstract Classes
Object Oriented System Design Class Diagrams
A Level Computer Science Topic 6: Introducing OOP
Presentation transcript:

Object Oriented Programming in A Level Session 4: Subclasses and Inheritance

Course Outline Inheritance and overriding More class diagrams Session Topics 1 Use of objects and methods Decomposition and abstraction Aims of learning OOP 2 Declaring classes Attributes, methods and the constructor 3 Composition (or Interaction) of classes Classes diagrams; visibility 4 Inheritance and overriding More class diagrams 5 Polymorphism and overloading Teaching issues Review and practical examples

Session Aim and Outline Aims Outline Understand the inheritance relationship between classes Be able to define a subclass Be able to show inheritance in UML class diagrams Recap week 3 Review of homework Inheritance: UML and code Practical break Review of 20Q example Discussion: Reuse and Decomposition Enhancing 20Q Summary

Recap of Week 3 Decomposition into classes  classes interact Object as an attribute Object passed as a parameter … object returned UML class diagrams Show relationships between classes Analysis and design Composition: a whole-part relationship Interaction is hard to understand at first

Program Structure versus Design Style x> x = 3 print initialise x for every x find Pseudo Flow If & loops x = while x > : y = print main init process save input … Call Graph Functions Function def Main program Initialise vars Call functions Friend name phone num setNumber sendText Town location getDirect setRating Class Diagram class Friend: def __init__() def m1(a, b): class Town: Classes & objects Main program Create obj Call methods

Program Structure versus Design Style If & loops x = while x > : y = print Program grows more complex in structure Simpler elements remain If & loop  part of function Method  part of class Functions Function def Main program Initialise vars Call functions class Friend: def __init__() def m1(a, b): class Town: Classes & objects Main program Create obj Call methods

Program Structure versus Design Style x> x = 3 print initialise x for every x find Pseudo Flow Design becomes more abstract Between problem and solution Concrete design still may be needed Design notations only show some information main init process save input … Call Graph Friend name phone num setNumber sendText Town location getDirect setRating Class Diagram

Review of Homework

A form can have several tests Mark is awarded for a test A test is set for a form A form can have several tests Mark is awarded for a test Mark is achieved by a pupil -topic -max -score * achieved 1 * setFor 1 Pupil 1 in * Form -name -age -roomNumber 1 teaches Teacher -name

Introducing Inheritance

Problem How do we prevent duplication of code? Functions Name: the code has a name so it can be (re)used Parameters: the code is adapted with parameters, so it is reusable in a new context Classes: what if two classes are similar? Some attributes the same Some methods similar Also need to use and adapt classes Use: inheritance Adapt: overriding

A teacher is a kind of person, with a job title and department Class Diagrams Person -firstName -lastName -email +fullName A person has a first and last name and an email. We know how to get the full name of person A teacher is a kind of person, with a job title and department tutor Pupil Teacher -keyStage -department -jobTitle A pupil is a kind of person, at some KS. A pupil has a tutor, who is Teacher

Words Person Pupil is a subclass (or subtype or child) of Person Person is the super-class (or super- type or parent) of Pupil Pupil extends Person Pupil inherits from Person Pupil is a specialisation of Person Pupil is a kind of Person Pupil

Quiz: Class Diagrams Person -firstName -lastName -email +fullName Does a teacher have a job title? Do all people have a department? Does a pupil have an email? Does a pupil have a department? Can a pupil be a tutor? Give an example of inheritance Give an example of association tutor Pupil Teacher -keyStage -department -jobTitle

Class Declaration Person -firstName -lastName -email +fullName tutor class Person: def __init__(self,f,l,e): self.firstName = f self.lastName = l self.email = e def fullName(self): fn = self.firstName + " " fn += self.lastName fn += ", email: " fn += self.email return fn tutor Pupil Teacher -keyStage -department -jobTitle

Sub Class Declaration Person -firstName -lastName -email +fullName Name of parent class -firstName -lastName -email +fullName Use super() to run constructor of parent class Always start with this class Teacher(Person): def __init__(self,f,l,e,d,j): super().__init__(f,l,e) self.department = d self.jobTitle = j tutor class Pupil(Person): def __init__(self,f,l,e,k): super().__init__(f,l,e) self.keyStage = k self.tutor = None def setTutor(self, t): self.tutor = t Pupil Teacher -keyStage -department -jobTitle

Inheritance Summary Attributes Methods Python: half-truths The subclass can add new attributes The attributes of the super-class also exist Methods The subclass can add new methods The methods of the super-class also exist BUT … the sub-class can change their behaviour Python: half-truths

Overriding Changing the behaviour of a method in a sub-class

Overriding What is the full name of a Teacher? Person -firstName -lastName -email +fullName What is the full name of a Teacher? Every person has a fullName Behaviour inherited from Person We want different behaviour in the two subclasses Pupil tutor -keyStage terry = Teacher("Terry","Smith", "tsmith@gmail","ICT", "Computing Teacher") paul = Pupil("Paul","Morris", "p.morris12@school.org","KS3") Paul Morris, email: p.morris12@school.org, KS3 Terry Smith, email: tsmith@gmail, Title: Computing Teacher Teacher -department -jobTitle

Overriding Person Use super() to run method from parent class (optional) -firstName -lastName -email +fullName class Teacher(Person): ... def fullName(self): fn = super().fullName() fn += ", Title: " + self.jobTitle return fn Pupil tutor -keyStage class Pupil(Person): ... def fullName(self): fn = super().fullName() fn += ", " + self.keyStage return fn Teacher -department -jobTitle

Further Topics (Next Week) More on overriding What must stay the same / can change Abstract methods and classes Checking between class and sub-class What is polymorphism and how does it work? What is overloading?

DEMONSTRATION Activity sheet: 20Q 20Q classic version: One player chooses a secret object. The other player has 20 questions to guess the object This version: the computer has: a (small) fixed set of objects (which are animals) a fixed set of questions with an answer for each object The game is played: the computer chooses on of the objects – a secret … and displays the possible objects and the questions the player chooses a question and gets the answer after a number of questions, the player must guess … If the guess is wrong, the correct answer is displayed DEMONSTRATION

Activity Sheet 20Q: Exercise 1 Exercise 1: run and review provided code

Class Diagram The Animal subclasses

Overriding and Inheritance Methods: Implemented, inherited or overridden Method Classes Animal Mammal MarineMammal hasFur Implemented Overridden canSwim Inherited getSize getLegs

Overriding and Inheritance Explain the way that: The function canSwim() returns a different value for a Tiger and a Dolphin The function getSize() returns a different value for a Tiger and Badger The method canSwim() returns a different value in Tiger and Dolphin because Tiger is a Mammal wheras Dolphin is a MarineMammal. The canSwim() method is overridden in the MarineMammal class to return true. The method getSize() accesses the value of the size attribute which is set in the constructor of the Animal (and all its subclasses). The getSize() method is not overridden: the implementation in the Animal class is inherited by all the subclasses. Instead, the two Mammals have different size values given in the call to the constructor.

Discussion: OOP and Computation Thinking What Computational Thinking Words Describe OOP?

OO and CT Decomposition Abstraction Patterns Other words Program made up of classes … objects work together Abstraction Class represent an abstraction Super-class abstract common behaviour Patterns Inheritance and over-riding Patterns of classes Other words Generalisation Reuse From https://barefootcas.org.uk/barefoot-primary-computing-resources/concepts/computational-thinking/

Activity Sheet 20Q: More Exercises

Concepts and Summary

OOP Concepts Concept Details Basic mechanics Calling a method of an object Class as a template for data Class as a collection of methods Constructors Definition and use Interaction Object as a value (variable, list item, …) Object as an attribute value (has-a relationship) Object passed as a parameter Abstraction and modelling Class as a domain concept Methods (and constructor) have parameters Inheritance Superclass and subclasses Constructor called using super() Method inherited or overridden Prerequisite knowledge: functions & parameters Prerequisite knowledge: basic mechanisms

Summary Classes overlap? A subclass Always call the constructor Abstract (or generalise) common behaviour in a superclass Specialise in sub-classes A subclass Add attributes (or methods) Overrides methods Always call the constructor

Introduction to Homework

Homework Task 1 Task 2 No more animals! Create a new 20Q topic. Suggest base on code for Tasks 1-3 Task 2 Bring the animals back! Use the Task 4 code to combine your new topic with animals Randomly choose a topic