Download Understanding Objects, Classes, and Inheritance in OOP with UML and Java and more Slides Banking and Finance in PDF only on Docsity!
Object-Oriented Software Engineering
Practical Software Development using UML and Java
Chapter 2: Review of Object Orientation
2
2.1 What is Object Orientation?
Procedural paradigm:
- Software is organized around the notion of procedures
- Procedural abstraction —Works as long as the data is simple
- Adding data abstractions —Groups together the pieces of data that describe some entity —Helps reduce the system’s complexity. - Such as Records and structures
Object oriented paradigm:
- Organizing procedural abstractions in the context of data abstractions
3
Object Oriented paradigm
An approach to the solution of problems in which all computations are performed in the context of objects.
- The objects are instances of classes, which: —are data abstractions —contain procedural abstractions that operate on the objects
- A running program can be seen as a collection of objects collaborating to perform a given task
4
A View of the Two paradigms
See in Umple
5
2.2 Classes and Objects
Object
- A chunk of structured data in a running software system
- Has properties —Represent its state
- Has behaviour —How it acts and reacts —May simulate the behaviour of an object in the real world
6
Objects
7
Classes
A class:
- A unit of abstraction in an object oriented (OO) program
- Represents similar objects —Its instances
- A kind of software module —Describes its instances’ structure (properties) —Contains methods to implement their behaviour
8
Is Something a Class or an Instance?
- Something should be a class if it could have instances
- Something should be an instance if it is clearly a single member of the set defined by a class Film
- Class; instances are individual films. Reel of Film :
- Class; instances are physical reels Film reel with serial number SW
- Instance of ReelOfFilm Science Fiction
- Instance of the class Genre. Science Fiction Film
- Class; instances include ‘Star Wars’ Showing of ‘Star Wars’ in the Phoenix Cinema at 7 p.m .:
- Instance of ShowingOfFilm
9
Naming classes
- Use capital letters —E.g. BankAccount not bankAccount
- Use singular nouns
- Use the right level of generality —E.g. Municipality, not City
- Make sure the name has only one meaning —E.g. ‘bus’ has several meanings
10
2.3 Instance Variables
Variables defined inside a class corresponding to data present in each instance
- Also called fields or member variables
- Attributes —Simple data —E.g. name, dateOfBirth
- Associations —Relationships to other important classes —E.g. supervisor, coursesTaken —More on these in Chapter 5
11
Variables vs. Objects
A variable
- Refers to an object
- May refer to different objects at different points in time
An object can be referred to by several different variables at the same time
Type of a variable
- Determines what classes of objects it may contain
12
Class variables
A class variable’s value is shared by all instances of a class.
- Also called a static variable
- If one instance sets the value of a class variable, then all the other instances see the same changed value.
- Class variables are useful for: —Default or ‘constant’ values (e.g. PI) —Lookup tables and similar structures
Caution: do not over-use class variables
13
2.4 Methods, Operations and Polymorphism
Operation
- A higher-level procedural abstraction that specifies a type of behaviour
- Independent of any code which implements that behaviour —E.g. calculating area (in general)
14
Methods, Operations and Polymorphism
Method
- A procedural abstraction used to implement the behaviour of a class
- Several different classes can have methods with the same name —They implement the same abstract operation in ways suitable to each class —E.g. calculating area in a rectangle is done differently from in a circle
15
Polymorphism
A property of object oriented software by which an abstract operation may be performed in different ways in different classes.
- Requires that there be multiple methods of the same name
- The choice of which one to execute depends on the object that is in a variable
- Reduces the need for programmers to code many if-else or switch statements
16
2.5 Organizing Classes into Inheritance
Hierarchies
Superclasses
- Contain features common to a set of subclasses
Inheritance hierarchies
- Show the relationships among superclasses and subclasses
- A triangle shows a generalization
Inheritance
- The implicit possession by all subclasses of features defined in its superclasses
17
An Example Inheritance Hierarchy
Inheritance
- The implicit possession by all subclasses of features defined in its superclasses
See in Umple
18
The Isa Rule
Always check generalizations to ensure they obey the isa rule
- “A checking account is an account”
- “A village is a municipality”
Should ‘ Province ’ be a subclass of ‘ Country ’?
- No, it violates the isa rule —“A province is a country” is invalid!
19
A possible inheritance hierarchy of
mathematical objects
20
Make Sure all Inherited Features Make
Sense in Subclasses
21
2.6 Inheritance, Polymorphism
and Variables
22
Some Operations in the Shape Example