Review of object Orientation Lecturer: Dr. Mai Fadel.

25
Review of object Orientation Lecturer: Dr. Mai Fadel

Transcript of Review of object Orientation Lecturer: Dr. Mai Fadel.

Page 1: Review of object Orientation Lecturer: Dr. Mai Fadel.

Review of object Orientation

Lecturer: Dr. Mai Fadel

Page 2: Review of object Orientation Lecturer: Dr. Mai Fadel.

What is Object Orientation?

• Object-oriented systems make use of abstraction in order to help make software less complex

• Abstraction: a representation that captures only essential aspects of something, reducing the complexity apparent to the abstraction’s user

• Object-oriented systems combine procedural and data abstraction

Page 3: Review of object Orientation Lecturer: Dr. Mai Fadel.

What is Object Orientation?- Procedural Paradigm

• Software has been organized around the notion of procedures (functions/routines)

• A programmer does not need to worry about all the details of how it performs its computation

• focus on how to call the procedure and what it computes• The entire system is organized into a set of procedures

with one ‘main’ procedure calls several other procedures• Works well for performing calculations with relatively

simple data• Become complex if each procedure works with many

types of data, or if each type of data has many different procedures that access and modify it

Page 4: Review of object Orientation Lecturer: Dr. Mai Fadel.

What is Object Orientation? - Data Abstraction

• Records and structures were the first data abstractions to be introduced

• To group together the pieces of data that describe some entity, so that programmers can manipulate that data as a unit

• Problems: – complex code in many different places– Implementation of common rules would be scattered

throughout the code making change very difficult

Page 5: Review of object Orientation Lecturer: Dr. Mai Fadel.

Data abstraction – Banking example

• Design: a hierarchy of procedures + records representing banking accounts

• Functionality:– Manage accounts of different types, such as

checking/current, savings, mortgage accounts– Each type of account will have different rules for

computation of fees, interest, etc.– Different account holders might have different rights

• Problems– Frequent problematic pseudo codes– Rules scattered throughout the code

Page 6: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Object-Oriented Paradigm

• Organizing procedural in the context of data abstractions• The root of Object-oriented (OO) paradigm is to putt all

the procedures that access or modify a particular class of objects in one place

• DefinitionThe Object-oriented paradigm is an approach to

the solution of problems in which all computations are performed in the context of objects. The objects are instances of programming constructs, normally called classes, which are data abstractions and which contain procedural abstractions that operate on the objects

• A running system can be seen as a collection of objects collaborating to perform a given task

Page 7: Review of object Orientation Lecturer: Dr. Mai Fadel.

+credit()+debit()

Account

+computeInterest()+computeFees()

CurrentAccount

+computeInterst()+computeFees()

SavingAccount

performTransaction

credit debit computeInterestIf currect then xxxif savings then xxxect.

computeFeesIf currect then xxxif savings then xxx

ect.

main

Procedural Object-oriented

Page 8: Review of object Orientation Lecturer: Dr. Mai Fadel.

Classes and Objects

• Object: can represent anything with which you can associate properties and behaviour– Properties characterize the object, describing

its current state– Behaviour is the way an object acts and

reacts, possibly changing its state– e.g. an example of a banking system

Page 9: Review of object Orientation Lecturer: Dr. Mai Fadel.

-dateOfBirth : String-address : String-position

Employe

-balance-opened-property

Account

-amount-time

Transaction

-location

InstantTeller

dateOfBirth = "1955/02/02"address = "99 UML St."position = "Manager"

Jane

dateOfBirth = "1970/01/01"address = "75 Object Dr."position

Greg

balance = 1796.32opened = "1999/03/03"property

Saving accounts 12876 : Account

dateOfBirth = "1984/03/03"address = "150 C++ Rd."position = "Teller"

Margeret

balance = 198760.00opened = "2003/08/12"property = "75 Object Dr."

Mortgage account 29865 : Account

amount = 200.00time = "2001/09/01 14:30"

Transaction 487 : Transaction

location = "Java Valley Cafe"

Instant Teller 876 : InstantTeller

Payroll system: an employee

University registration program: student, course, faculty

Factory automation system: assembly line, each robot, each item, type of product

Page 10: Review of object Orientation Lecturer: Dr. Mai Fadel.

Classes and their instances

• Classes: are the units of data abstraction in an object-oriented program

• All the objects with the same properties and behaviour are instances of one class

• Class Employee declares that all its instances have a name, a dateOfBirth, an address, and a position

• A class contains all of the code that relates to its objects, including– Code describing how the objects of the class are structured- i.e. the data stored in each object that implement the properties– The procedures, called methods, that implement the behaviour of the object.

Employee

namedateOfBirthaddressposition

Page 11: Review of object Orientation Lecturer: Dr. Mai Fadel.

Example 2.1 & Exercise

• Examples of classes and instances– Film– Reel of film– Film reel with serial

number SW19876– Showing of ‘Star Wars’

in Phoenix Cinema at 7 pm

• Exercise– General Motors– Game– Automobile Company– The game of chess

between Tom and Jane which started at 2:30 pm yesterday

Page 12: Review of object Orientation Lecturer: Dr. Mai Fadel.

Naming Classes

• How to name a class? Important conventions to help the reader differentiate between what is class & what is not– First Letter Capital– Written in Singular (a single Item not a list)– More than one name: Bumpy capitals, e.g. PartTimeEmployee

– Meaning: neither too general nor too specific– Name classes after the thing their instances represent– Avoid using words that reflect the internals of a

computer systems. e.g. ‘Record’, ‘Structure’,’Data’– Refer to pp. 34-35

Page 13: Review of object Orientation Lecturer: Dr. Mai Fadel.

Exercise

• Not good names for classes in a scheduling system of a passenger rail company– Train- Stop- SleepingCareData- arrive-

Routes- driver- SpecialTrainInfo

• Identify all classes that might be part of the following systems– A restaurant reservation system– A video rental store

Page 14: Review of object Orientation Lecturer: Dr. Mai Fadel.

Instance Variables (IV)s- A variable is a place where you can put data.- IVs are the list of variables of a class that will be present in each instance- IVs are used to implement attributes or associations

Attributes • An attribute is a piece of

data used to represent the properties of an object

• e.g. Employee: name, dateOfBirth, socialSecurityNumber, telephoneNumber, address

Associations• An association represents

the relationship between instances of one class and instances of another

• e.g. Employee– Manager (supervisor)– Task (tasksToDo)

Page 15: Review of object Orientation Lecturer: Dr. Mai Fadel.

Instance Variables (IV)s

• Var. vs. Objects: A variable is often called a reference when it refers to an object

• A class variable/ static variable is created to hold a value that is shared by all instances of a class – Usually used to define ‘constant’ values, and lookup

tables used by algorithms inside a particular class

• Identify the attributes– Passenger (in an airline system)– PhoneCall (in the system of a mobile phone

company)

Page 16: Review of object Orientation Lecturer: Dr. Mai Fadel.

Methods, Operations and Polymorphism

• Method ≈ procedure, function, or routine in programs• Methods are procedural abstractions used to implement

the behaviour of a class• An operation is used to discuss and specify a type of

behaviour, independently of any code that implements that behaviour (higher abstraction)

• Polymorphic operation is an operation that changes its behaviour during run-time. The program decides which of several identically named methods to invoke.

• Polymorphism is a property of OO software by which an abstract operation may be performed in different ways, typically in different classes

• e.g. computing the Perimeter of a rectangular, & a square

Page 17: Review of object Orientation Lecturer: Dr. Mai Fadel.

Organizing Classes into inheritance Hierarchies

• If several classes have attributes, associations, or operations in common, it is best to avoid duplication by creating a separate superclass that contains these common aspects.

• If you have a complex class, it may be good to divide its functionality among several specialized subclasses

• A generalization is the relationship between a subclass and its immediate superclass

• A hierarchy with one or more generalizations is called an inheritance hierarchy/ generalization hierarchy/ isa hierarchy

Page 18: Review of object Orientation Lecturer: Dr. Mai Fadel.

Inheritance• Inheritance is the implicit

possession by a subclass of features defined in a superclass. Features include variable and methods

• Inheritance automatically occurs

• The isa rule: class A can only be a valid subclass of class B if it makes sense, in English, to say ‘an A is a B’

• 3 checks pp. 40-41• Careful generalizations and

their resulting inheritance hierarchies help to avoid duplication and improve reuse

• Diagram form

• Textual formAccount

SavingAcc CurrentAcc MortgageAcc

Account

SavingsAcc CurrentAcc MortgageAcc

Page 19: Review of object Orientation Lecturer: Dr. Mai Fadel.

MathematicalObject

Shape Point Matrix

Shape2D Shape3D

Ellipse Polygon

Circle Quadrilateral

Rectangle

PlaneLine

Example 2.2: Organize the following set of classes into hierarchies: Circle, Point, Rectangle, Matrix, Ellipse, Line, Plane

EllipseShape

Ellipsefocus1focus2

Circlecenter

Page 20: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Effect of Inheritance on Polymorphism and Variable Declarations

translate()getCenter()rotate()changeScale()getArea()getPerimeterLength()getBoundingRect()

center

Shape2D

semiMajorAxis

EllipticalShape

getBoundingRect()getVertics()

Polygon

rotate()changeScale()getArea()getPerimeterLength()getBoundingRect()getOrientation()getSemiMajorAxis()getSemiMinorAxis()getFocus1()getFocus2()

semiMinorAxisorientation

Ellipse

rotate()changeScale()getArea()getPerimeterLength()getBoundingRect()getRadius()

Circle

rotate()getOrientation()

orientation

SimplePolygon

addPoint()removePoint()rotate()changeScale()getArea()getPerimeterLength()getVertics()

points

ArbitraryPolygon

changeScale()setHeight()setWidth()getArea()getPerimeterLength()getVetics()getBoundingRect()

heightwidth

Rectangle

changeNumPoints()changeScale()getArea()getPerimeterLength()getVertics()

numPointsradius

RegularPolygon

Page 21: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Effect of Inheritance on Polymorphism and Variable Declarations

• Much of the power of the OO paradigm comes from polymorphism and inheritance working together

• Figure 2.8: four-level hierarchy, leaf classes (pp. 45-47)

Page 22: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Effect of Inheritance on Polymorphism..- Abstract classes and abstract methods

• rotate operation found in Shape2D class is an abstract operation. Why? p. 47– No method for that operation exists in the class– Has logical meaning in class– Abstract operations are shown in italics– You have abstract operations anywhere except leaf classes

• Shape2D, EllipticalShape, Polygon, SimplePolygon, must be abstract classes. Why? p. 47– It cannot have any instances– Any class except leaf classes can be declared abstract– The class that has one or more abstract methods must be declared

abstract– The main purpose of an abstract class is to hold features that will be

inherited by its subclasses– If a class is not abstract it is called concrete– In concrete classes, instances can be created, all leaf classes must be

concrete, it is possible to have concrete classes at higher-levels• The implementation of abstract operations is completed by the time we

reach the leaf classes. The implementation can be distributed along the hierarchy from superclass till leaf class (rotate in SimplePolygon)

• More details in p.48

Page 23: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Effect of Inheritance on Polymorphism..- Overriding

• The implementation of getBoundingRect() in Rectangle overrides the implementation of the same operation in Polygon

• Three reasons for overriding: – Restriction: the overriding method prevents a violation of a

certain constraints that are present in the subclass, but were not present in the superclass.

• It can have some undesirable effects p.49• It is important to ensure that all polymorphic methods implementing

an abstract operation behave consistently

– Extension: the overriding method does basically the same thing as the version in the superclass, but adds some extra capability needed in the subclass

– Optimization: the overriding method in thesubclass has exactly the same effect as the overridden method, except that it is more efficient.

Page 24: Review of object Orientation Lecturer: Dr. Mai Fadel.

The Effect of Inheritance on Polymorphism..- Variable & Dynamic binding

• Declaring a variable of a superclass means that as the program runs, the variable can contain objects of any concrete class in the hierarchy.

• If you attempt to invoke a polymorphic operation on the variable, the program will make the decision about what method to run ‘on the fly’.

• Dynamic binding/ late binding/ virtual binding is the decision-making process.

• The procedure used to perform dynamic binding p.50• For efficiency an optimized approach using a lookup

table is used instead• Dynamic binding is only needed when the compiler

determines that there is more than one possible method that could be executed by a particular call

Page 25: Review of object Orientation Lecturer: Dr. Mai Fadel.

Dynamic binding –exercise

• In which of the following would dynamic binding be needed?

• Variable of type invoke the operation

Rectangle getPerimeterLength

SimplePolygon getCentre

Polygon getBoundingRect