0
Inheritance & UML More Advanced UML
Objectives <ul><li>Inheritance & Hierarchies </li></ul><ul><li>Introduction to UML </li></ul><ul><li>Drawing UML Diagrams ...
Relationships between classes <ul><li>Two classes are independent </li></ul><ul><ul><li>Nothing in common </li></ul></ul><...
Inheritance hierarchy
Inheritance <ul><li>Mechanism by which one class acquires the properties of another class </li></ul><ul><li>Base class  – ...
Basic class description <ul><li>A class is drawn as a box with compartments for </li></ul><ul><ul><li>class name </li></ul...
Inheritance Representation (UML) <ul><li>UML = Unified Modeling Language </li></ul><ul><li>Helps you specify, visualize, a...
Composition <ul><li>An object is a  data member  of a class </li></ul><ul><li>Mechanism by which the internal data of one ...
Different UML Diagrams <ul><li>Structural Diagrams (represent static application structure) </li></ul><ul><ul><li>Class Di...
Problem Analysis: Game <ul><li>You are a frog. Your task is simple: hop across a busy highway, dodging cars and trucks, un...
What are the Objects (Classes)? <ul><li>Step #1: Mark  nouns </li></ul><ul><li>You are a  frog . Your task is simple: hop ...
What are the Operations/Methods? <ul><li>Step #2: Mark  verbs </li></ul><ul><li>You are a frog. Your task is simple:  hop ...
Class Diagram 1: Frog <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>hop () leap (...
Class Diagram 2: Highway <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>X: integer...
Class Diagram 3: Truck <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>drive () X: ...
Define Operation Visibility <ul><li>Operation visibility is used to enforce  Encapsulation </li></ul><ul><li>Also allows f...
How Is Visibility Noted? <ul><li>The following symbols are used to specify export control: </li></ul><ul><ul><li>+ Public ...
Example: Define Operations Variable Name : Type Function Return Type Student - name : String - dateOfBirth : Date + canEnr...
Class Relationship <ul><li>Is-A </li></ul><ul><li>Aggregation  &  Composition </li></ul><ul><ul><li>Aggregation can be sha...
Example:  Single Inheritance <ul><li>One class inherits from another </li></ul>Truck tonnage GroundVehicle weight licenseN...
What is Multiple Inheritance? <ul><li>A class can inherit from several other classes </li></ul>Use multiple inheritance on...
Multiple Inheritance: Problems Name clashes  on  attributes or operations Repeated inheritance FlyingThing color getColor ...
Inheritance vs. Aggregation <ul><li>Inheritance and aggregation are often confused </li></ul><ul><li>Inheritance represent...
Example: Composition (has-a) notifyOfferingSelection(offering : CourseOffering) RegistrationController getOfferings(curric...
Generalization to Support Polymorphism Without Polymorphism With Polymorphism <ul><li>if animal = “Lion” then </li></ul><u...
Upcoming SlideShare
Loading in...5
×

09 inheritance and_uml

253

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
253
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 02/16/11 Operation visibility is the realization of the key object-orientation principle of encapsulation. Public members are accessible directly by any client. Protected members are directly accessible only by instances of subclasses. Private members are directly accessible only by instances of the class to which they are defined. How do you decide what visibility to use? Look at the interaction diagrams on which the operation is referenced. If the message is from outside of the object, use public. If it is from a subclass, use protected. If it’s from itself, use private. You should define the most restrictive visibility possible that will still accomplish the objectives of the class. Client access should be granted explicitly by the class and not taken forcibly. Visibility applies to attributes as well as operations. Attributes are discussed later in this module. UML defines only public/protected/private visibility, not implementation visibility. Implementation visibility should be defined as a property string (UML 1.1, Notation Guide 5.7.2) Re-emphasize that you must define a good abstraction – you must think about what operations are public and private and you must think about encapsulation. It must be done well here for it to be good at the package level.
  • 02/16/11 In the UML, you can specify the access clients have to attributes and operations. Export control is specified for attributes and operations by preceding the name of the member with the following symbols: + Public # Protected - Private Note that the UML 1.1 Notation Guide explicitly states “A tool may show the visibility indication in a different way, such as by using a special icon …” so the usage of the graphical icons for export control is valid UML.
  • 02/16/11 In the above example, the student can answer the question of whether it can take a particular offering of a course or not. We must read the description of the operation to understand what rules are considered (e.g. does student’s past schedules include a passing grade for all the courses prerequisites, does students current schedule have a time conflict). If a new rule was added later (e.g. undergraduates can’t take graduate courses) it would go here. That is the value of having an operation that encapsulates a business task (or rule) within a class.
  • 02/16/11 클래스들 간의 관계는 크게 세가지로 볼 수 있다 . 먼저 계승을 나타내는 IS-A 관계 , member attribute 를 나타내는 aggregation 관계 , 그리고 이 둘이 아닌 일반적인 관계를 들 수 있다 . 이전 단계에서 만든 object diagram 을 보고 이러한 관계성을 찾아내어 클래스 다이어그램에 표시한다 .
  • 02/16/11 Inheritance is much more than just finding common attribute, operations and relationships. It should be more about the responsibilities and essence of the classes. When building the generalization hierarchies, you should avoid “skyscrapers”; the hierarchies should look like small, independent “forests”. This is general guidance; there may be reasons to break this guideline, just do so consciously. Remember that many levels in an generalization hierarchy affect the understandability of the model. See the Introduction to Object Orientation module for the more formal definition. Some languages do not support generalization. In these cases you will need to update the design model to reflect the characteristics of the implementation language. In cases where the implementation language does not support generalization between classes you must “design generalization in”. Emphasize what happens when a change is made to a super class – the fact that all descendent classes must inherit the change whether they want to or not. Generalization (single inheritance) was introduced in the Introduction to Object Orientation module. This slide was included here, as well, for review purposes.
  • 02/16/11 See the language-specific appendices for examples of how multiple inheritance is managed in programming environments that support it. Multiple inheritance means that a class can inherit from several other classes. In the above example, Bird inherits from both FlyingThing and Animal. Multiple inheritance is conceptually straightforward and sometimes necessary for modeling the real world accurately. However, there are some potential implementation problems when you use multiple inheritance, and not all implementation languages support it. Thus, be judicial with your use of multiple inheritance. Use it where it accurately describes the concept you are trying to model and reduces the complexity of your model, but be aware that this representation will probably need to be adjusted in design and implementation. Generally, a class inherits from only one class.
  • 02/16/11 In practice, multiple inheritance is a complex design problem and may lead to implementation difficulties. Two issues associated with multiple inheritance are: 1) Name collisions - both ancestors have attributes and/or operations with the same name, and 2) Repeated inheritance - descendents end up with two copies of an ancestor. See the language-specific appendices for examples of how the multiple inheritance problems are addressed in programming environments that support multiple inheritance.
  • 02/16/11 The keywords “is a” and “part of” help to determine correct relationship. With Inheritance: Key words “is-a” One object With Aggregation: Key words “part of” Relates multiple objects
  • 02/16/11 The above example demonstrates how a previous association relationship has been refined into a composition relationship. It was decided that an instance of a RegistrationController would NEVER exist outside the context of a particular Register For Courses Student session. Thus, since the MaintainScheduleForm represents a particular Register For Courses session, a RegistrationController would NEVER exist outside of the context of a particular MaintainScheduleForm. When a MaintainScheduleForm is created, an instance of RegistrationController should always be created. When MaintainScheduleForm is closed, the instance of the RegistrationController should always be deleted. Thus, because they now have coincident lifetimes, composition is used instead of an association.
  • 02/16/11 Inheritance provides a way to implement polymorphism in cases where polymorphism is implemented the same way for a set of classes. This means that abstract base classes that simply declare inherited operations, but which have no implementations of the operations, and no attributes or associations can be replaced with interfaces. Inheritance now can be (but need not be) restricted to inheritance of implementations only. Polymorphism is not generalization; generalization is one way to implement polymorphism. Polymorphism via generalization is the ability define alternate methods for operations of the ancestor class in the descendent classes. This can reduce the amount of code to be written, as well as help abstract the interface to descendent classes. Polymorphism is an advantage of inheritance realized during implementation and at run-time. Programming environments which support polymorphism use dynamic binding, meaning that the actual code to execute is determined at run-time vs. compile-time. It may be helpful to discuss dynamic binding at this poin; however, try not to get too bogged down in implementation language discussions. Normally the particular method to be executed as a result of a function call is known at compile time. This is called static (or early) binding. The compiler replaces the function call with code telling the program which address to jump to in order to find that function. With polymorphism, the particular type of object for which a method is to be invoked is not known until run time. The compiler cannot provide the address at compile time. The method is selected by the program as it is running This is known as late binding or dynamic linking. See the language-specific appendices for how this is handled by specific programming languages.
  • Transcript of "09 inheritance and_uml"

    1. 1. Inheritance & UML More Advanced UML
    2. 2. Objectives <ul><li>Inheritance & Hierarchies </li></ul><ul><li>Introduction to UML </li></ul><ul><li>Drawing UML Diagrams </li></ul>
    3. 3. Relationships between classes <ul><li>Two classes are independent </li></ul><ul><ul><li>Nothing in common </li></ul></ul><ul><li>Two classes are related by inheritance </li></ul><ul><li>Two classes are related by composition </li></ul>
    4. 4. Inheritance hierarchy
    5. 5. Inheritance <ul><li>Mechanism by which one class acquires the properties of another class </li></ul><ul><li>Base class – class being inherited from </li></ul><ul><li>Derived class – the class that inherits </li></ul><ul><ul><li>Is-a : every inherited member (data and functions) of the base class is also a member of the derived class </li></ul></ul><ul><ul><ul><li>A spoon is-a utensil </li></ul></ul></ul>
    6. 6. Basic class description <ul><li>A class is drawn as a box with compartments for </li></ul><ul><ul><li>class name </li></ul></ul><ul><ul><li>data members (attributes) </li></ul></ul><ul><ul><li>function members (operations) </li></ul></ul>
    7. 7. Inheritance Representation (UML) <ul><li>UML = Unified Modeling Language </li></ul><ul><li>Helps you specify, visualize, and document models of object-oriented software systems, including their structure and design. </li></ul><ul><li>More information: http://www. omg .org </li></ul>
    8. 8. Composition <ul><li>An object is a data member of a class </li></ul><ul><li>Mechanism by which the internal data of one class includes an object of another class </li></ul><ul><li>Has-a relationship </li></ul><ul><ul><li>A car has-a engine </li></ul></ul><ul><ul><ul><li>A car is-a engine? No! </li></ul></ul></ul>
    9. 9. Different UML Diagrams <ul><li>Structural Diagrams (represent static application structure) </li></ul><ul><ul><li>Class Diagram ( examples ) </li></ul></ul><ul><ul><li>Object Diagram ( examples ) </li></ul></ul><ul><ul><li>Component Diagram </li></ul></ul><ul><ul><li>Deployment Diagram </li></ul></ul><ul><ul><li>http://bdn.borland.com/article/0,1410,31863,00.html </li></ul></ul><ul><li>Behavior Diagrams (represent different aspects of dynamic behavior) </li></ul><ul><ul><li>Use Case Diagram </li></ul></ul><ul><ul><li>Sequence Diagram </li></ul></ul><ul><ul><li>Activity Diagram </li></ul></ul><ul><ul><li>Collaboration Diagram </li></ul></ul><ul><ul><li>Statechart Diagram </li></ul></ul><ul><li>Model Management Diagrams (ways you can organize and manage your application modules) </li></ul><ul><ul><li>Packages </li></ul></ul><ul><ul><li>Subsystems </li></ul></ul><ul><ul><li>Models </li></ul></ul>
    10. 10. Problem Analysis: Game <ul><li>You are a frog. Your task is simple: hop across a busy highway, dodging cars and trucks, until you get the to the edge of a river, where you must keep yourself from drowning by crossing safely to your grotto at the top of the screen by leaping across the backs of turtles and logs. But watch out for snakes and alligators! (Sega, 1980) </li></ul>
    11. 11. What are the Objects (Classes)? <ul><li>Step #1: Mark nouns </li></ul><ul><li>You are a frog . Your task is simple: hop across a busy highway , dodging cars and trucks , until you get the to the edge of a river , where you must keep yourself from drowning by crossing safely to your grotto at the top of the screen by leaping across the backs of turtles and logs . But watch out for snakes and alligators ! </li></ul>
    12. 12. What are the Operations/Methods? <ul><li>Step #2: Mark verbs </li></ul><ul><li>You are a frog. Your task is simple: hop across a busy highway, dodging cars and trucks, until you get the to the edge of a river, where you must keep yourself from drowning by crossing safely to your grotto at the top of the screen by leaping across the backs of turtles and logs. But watch out for snakes and alligators! </li></ul>
    13. 13. Class Diagram 1: Frog <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>hop () leap () drown () X: integer Y: integer Frog
    14. 14. Class Diagram 2: Highway <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>X: integer Y: integer Highway
    15. 15. Class Diagram 3: Truck <ul><li>Class name </li></ul><ul><li>Attributes </li></ul><ul><li>Operations </li></ul>drive () X: integer Y: integer Direction: String Truck
    16. 16. Define Operation Visibility <ul><li>Operation visibility is used to enforce Encapsulation </li></ul><ul><li>Also allows for Information Hiding </li></ul><ul><li>Visibility may be public , protected , or private </li></ul>Public operations Protected operations Private operations
    17. 17. How Is Visibility Noted? <ul><li>The following symbols are used to specify export control: </li></ul><ul><ul><li>+ Public access </li></ul></ul><ul><ul><li># Protected access </li></ul></ul><ul><ul><li>- Private access </li></ul></ul>Class - privateAttribute # protectedAttribute +publicOp() # protectedOp() - privateOp()
    18. 18. Example: Define Operations Variable Name : Type Function Return Type Student - name : String - dateOfBirth : Date + canEnroll() : Boolean # hasTakenPrerequisites() : Boolean # hasScheduleConflict() : Boolean
    19. 19. Class Relationship <ul><li>Is-A </li></ul><ul><li>Aggregation & Composition </li></ul><ul><ul><li>Aggregation can be shared by others (Uses-A) whereas Composition implies it’s exclusive to it. </li></ul></ul><ul><li>Link (Association) </li></ul><ul><ul><li>Relationship which is not Is-A nor Has-A </li></ul></ul><ul><ul><ul><li>Between classes that exchange messages </li></ul></ul></ul>
    20. 20. Example: Single Inheritance <ul><li>One class inherits from another </li></ul>Truck tonnage GroundVehicle weight licenseNumber Car owner register( ) getTax( ) Person 0..* Trailer 1 ancestor decendent generalization size
    21. 21. What is Multiple Inheritance? <ul><li>A class can inherit from several other classes </li></ul>Use multiple inheritance only when needed, and always with caution ! Airplane Helicopter Wolf Horse FlyingThing Animal Bird multiple inheritance
    22. 22. Multiple Inheritance: Problems Name clashes on attributes or operations Repeated inheritance FlyingThing color getColor Animal color getColor Bird Resolution of these problems is implementation-dependent FlyingThing Animal Bird AnimateObject color
    23. 23. Inheritance vs. Aggregation <ul><li>Inheritance and aggregation are often confused </li></ul><ul><li>Inheritance represents an “is-a” or “kind-of” relationship </li></ul><ul><li>Aggregation represents a “part-of” relationship </li></ul>A WindowWithScrollbar “is a” Window A WindowWithScrollbar “has a” Scrollbar Scrollbar Window WindowWithScrollbar 1 1 Window WindowWithScrollbar Scrollbar
    24. 24. Example: Composition (has-a) notifyOfferingSelection(offering : CourseOffering) RegistrationController getOfferings(curriculum) new(context : SecureUser) saveSchedule(sched : Schedule) cancelSchedule(sched : Schedule) <<control>> MaintainScheduleForm displayOfferings() selectCurriculum() : Curriculum selectOffering() : CourseOffering save() cancel() update(changedItem : ISubject) displaySchedule() <<boundary>> 1 1
    25. 25. Generalization to Support Polymorphism Without Polymorphism With Polymorphism <ul><li>if animal = “Lion” then </li></ul><ul><ul><li>do the Lion talk </li></ul></ul><ul><li>else if animal = “Tiger” then </li></ul><ul><ul><li>do the Tiger talk </li></ul></ul><ul><li>end </li></ul>do the Animal talk Animal talk () Lion Tiger talk () talk ()
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×