Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of object Orientation Lecturer: Dr. Mai Fadel.

Similar presentations


Presentation on theme: "Review of object Orientation Lecturer: Dr. Mai Fadel."— Presentation transcript:

1 Review of object Orientation Lecturer: Dr. Mai Fadel

2 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

3 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

4 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

5 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

6 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 Definition The 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

7 ProceduralObject-oriented

8 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

9 Payroll system: an employee University registration program: student, course, faculty Factory automation system: assembly line, each robot, each item, type of product

10 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 name dateOfBirth address position

11 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

12 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

13 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

14 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)

15 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)

16 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

17 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

18 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 form Account SavingAcc CurrentAcc MortgageAcc Account SavingsAccCurrentAccMortgageAcc

19 MathematicalObject ShapePointMatrix Shape2DShape3D EllipsePolygon CircleQuadrilateral Rectangle PlaneLine Example 2.2: Organize the following set of classes into hierarchies: Circle, Point, Rectangle, Matrix, Ellipse, Line, Plane EllipseShape Ellipse focus1 focus2 Circle center

20 The Effect of Inheritance on Polymorphism and Variable Declarations

21 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)

22 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

23 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.

24 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

25 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


Download ppt "Review of object Orientation Lecturer: Dr. Mai Fadel."

Similar presentations


Ads by Google