1 / 21

Object–Orientated Design

Object–Orientated Design. OOP Design. Describe the following: a computer a university Usually the most natural way to describe a task is to list the entities involved in the task. These entities are referred to as objects . OOP Design. Objects

shamara
Download Presentation

Object–Orientated Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object–Orientated Design

  2. OOP Design Describe the following: • a computer • a university Usually the most natural way to describe a task is to list the entities involved in the task. These entities are referred to as objects.

  3. OOP Design Objects Objects in general have two important properties: • State • Behaviour

  4. OOP Design Object States An object contains certain information about itself: e.g. • a lecturer “knows” their name, address, age, courses they teach etc. • a student “knows” their name, address, age, ID, courses studied etc. • a lecture theatre “knows” its location, capacity etc.

  5. OOP Design The information that an object maintains determines its state. The individual components of information are known as the objects attributes. The attributes of an object may be “primitive” values such as integers, characters etc. e.g. a lecturer’s name, a student’s ID

  6. OOP Design The attributes of an object may also be other objects. e.g. a computer may have memory as an attribute but memory may also be an object within the system. The attributes of memory could include capacity, type etc. Object Behaviour Apart from maintaining information about itself an object is also capable of performing certain actions.

  7. OOP Design e.g. a lecturer can teach a class, mark assignments, set an examination paper a student can attend a lecture, complete an assignment, sit an exam etc. The actions that an object can perform are known as its behaviours. When applying an object-orientated design to a problem specification we identify objects, record their states and specify their behaviour.

  8. OOP Design Instances and Classes Generally we can distinguish between two types of object: • An instance • A class

  9. OOP Design Instances Instances correspond to individual entities in the application of interest: e.g. a lecturer called “Mr Jones” a student called “Cathy Smith”

  10. OOP Design Classes Classes correspond to classes or templates of an entity: e.g. lecturer is a class computer is a class student is a class Each instance in a system must be an instance of some class.

  11. OOP Design e.g. Mr Jones is an instance of a lecturer Cathy Smith is an instance of a student Definition of an Object An object must be an instance of some class. A class describes the type of information and behaviour that an object can have while an object contains specific information for each attribute in its class.

  12. OOP Design Super-Classes Looking closely at our lecturer and student classes it is clear there is some similarity between the states of lecturers and students. e.g. teachers and students “know” their names, addresses and ages In object-orientated design we can specify a super-class that contains all the attributes and behaviours that are common between classes.

  13. OOP Design Consider the super-class person. The attributes of the person class could be: • Name • Address • Age The behaviour of the person class could be: • Telling name • Telling address • Telling age

  14. OOP Design Now the lecturer and student classes are sub-classes of the personsuper-class. All sub-classes inherit the properties of their super-classes. An instance of a sub-class is also automatically an instance of its super-class. A super-class can also be a sub-class of another super-class etc.

  15. OOP Design This sub-class super-class relationship can lead to a class or inheritance hierarchy. Example Machine Applicance Vehicle Computer Van Car Truck Mini Delivery Limo Sports Dump Pickup

  16. OOP Design An object is not only a member of the class it is an instance of; it is also a member of any of its super-classes, as well as a member of any of the super-classes super-classes etc. This argument also applies to sub-classes; not only is a class a sub-class of its super-class but a sub-class of the super-classes super-classes. e.g. a pickup is also a truck, a vehicle and a machine.

  17. OOP Design Each sub-class is said to inherit the state and behaviour of its super-classes i.e. we specify common states and behaviours once in a super-class which any sub-class can then inherit.

  18. OOP Design Behaviour Overriding Problem: A shape class contains the length of two dimensions and the behaviour calculate_areaas attributes. Three sub-classes of this class are defined as Square, Rectangle and Triangle. Shape Square Rectangle Triangle

  19. OOP Design The calculate_area behaviour returns the area of the shape by multiplying one dimension by the other. What problem arises with this class hierarchy ? The inherited behaviour calculate_area will provide an incorrect answer for an object of class Triangle.

  20. OOP Design To overcome this problem we can remove the common behaviour from the class shape and incorporate separate behaviours in all three sub-classes Square, Rectangle and Triangle. Each sub-class class can contain the same behaviour named calculate_area. This is known as polymorphism. This method is perfectly legal but there is some duplication in behaviour particularly between the Rectangle and Square classes.

  21. OOP Design Fortunately Object-Orientated Design provides a more efficient and natural way to overcome this problem. A subclass can override the behaviour defined in a super-class i.e. if a specific behaviour is defined in a class as well as its super-class then the class behaviour takes priority. e.g. the generic behaviour calculate_area can be inherited by the Square and Rectangle classes but we can override the calculate_area behaviour in the Triangle class.

More Related