Inheritance & UML More Advanced UML
Objectives Inheritance & Hierarchies Introduction to UML Drawing UML Diagrams
Relationships between classes Two classes are independent Nothing in common Two classes are related by inheritance Two classes are related by composition
Inheritance hierarchy
Inheritance Mechanism by which one class acquires the properties of another class Base class  – class being inherited from Derived class  – the class that inherits Is-a : every inherited member (data and functions) of the base class is also a member of the derived class A spoon  is-a  utensil
Basic class description A class is drawn as a box with compartments for class name data members (attributes) function members (operations)
Inheritance Representation (UML) UML = Unified Modeling Language Helps you specify, visualize, and document models of object-oriented software systems, including their structure and design. More information:  http://www. omg .org
Composition An object is a  data member  of a class Mechanism by which the internal data of one class includes an object of another class Has-a  relationship A car  has-a  engine A car  is-a  engine?  No!
Different UML Diagrams Structural Diagrams (represent static application structure) Class Diagram ( examples ) Object Diagram ( examples )  Component Diagram Deployment Diagram http://bdn.borland.com/article/0,1410,31863,00.html   Behavior Diagrams (represent different aspects of dynamic behavior) Use Case Diagram Sequence Diagram Activity Diagram Collaboration Diagram Statechart Diagram Model Management Diagrams (ways you can organize and manage your application modules) Packages Subsystems Models
Problem Analysis: Game 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)
What are the Objects (Classes)? Step #1: Mark  nouns 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 !
What are the Operations/Methods? Step #2: Mark  verbs 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!
Class Diagram 1: Frog Class name Attributes Operations hop () leap () drown () X: integer Y: integer Frog
Class Diagram 2: Highway Class name Attributes Operations X: integer Y: integer Highway
Class Diagram 3: Truck Class name Attributes Operations drive () X: integer Y: integer Direction: String Truck
Define Operation Visibility Operation visibility is used to enforce  Encapsulation Also allows for  Information Hiding Visibility may be  public ,  protected , or  private Public operations Protected  operations Private operations
How Is Visibility Noted? The following symbols are used to specify export control: + Public access  # Protected access - Private access Class - privateAttribute # protectedAttribute +publicOp() # protectedOp() - privateOp()
Example: Define Operations Variable Name : Type Function Return Type Student - name : String - dateOfBirth : Date + canEnroll() : Boolean # hasTakenPrerequisites() : Boolean # hasScheduleConflict() : Boolean
Class Relationship Is-A Aggregation  &  Composition Aggregation can be shared by others (Uses-A) whereas Composition implies it’s exclusive to it. Link (Association) Relationship which is not Is-A nor Has-A Between classes that  exchange messages
Example:  Single Inheritance One class inherits from another Truck tonnage GroundVehicle weight licenseNumber Car owner register( ) getTax( ) Person 0..* Trailer 1 ancestor decendent generalization size
What is Multiple Inheritance? A class can inherit from several other classes Use multiple inheritance only when needed, and  always with caution ! Airplane Helicopter Wolf Horse FlyingThing Animal Bird multiple inheritance
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
Inheritance vs. Aggregation Inheritance and aggregation are often confused Inheritance represents an “is-a” or “kind-of” relationship Aggregation represents a “part-of” relationship A WindowWithScrollbar “is a” Window A WindowWithScrollbar “has a” Scrollbar Scrollbar Window WindowWithScrollbar 1 1 Window WindowWithScrollbar Scrollbar
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
Generalization to Support Polymorphism Without Polymorphism With Polymorphism if animal = “Lion” then do the Lion talk else if animal = “Tiger” then do the Tiger talk end do the Animal talk Animal talk () Lion Tiger talk () talk ()

09 inheritance and_uml

  • 1.
    Inheritance & UMLMore Advanced UML
  • 2.
    Objectives Inheritance &Hierarchies Introduction to UML Drawing UML Diagrams
  • 3.
    Relationships between classesTwo classes are independent Nothing in common Two classes are related by inheritance Two classes are related by composition
  • 4.
  • 5.
    Inheritance Mechanism bywhich one class acquires the properties of another class Base class – class being inherited from Derived class – the class that inherits Is-a : every inherited member (data and functions) of the base class is also a member of the derived class A spoon is-a utensil
  • 6.
    Basic class descriptionA class is drawn as a box with compartments for class name data members (attributes) function members (operations)
  • 7.
    Inheritance Representation (UML)UML = Unified Modeling Language Helps you specify, visualize, and document models of object-oriented software systems, including their structure and design. More information: http://www. omg .org
  • 8.
    Composition An objectis a data member of a class Mechanism by which the internal data of one class includes an object of another class Has-a relationship A car has-a engine A car is-a engine? No!
  • 9.
    Different UML DiagramsStructural Diagrams (represent static application structure) Class Diagram ( examples ) Object Diagram ( examples ) Component Diagram Deployment Diagram http://bdn.borland.com/article/0,1410,31863,00.html Behavior Diagrams (represent different aspects of dynamic behavior) Use Case Diagram Sequence Diagram Activity Diagram Collaboration Diagram Statechart Diagram Model Management Diagrams (ways you can organize and manage your application modules) Packages Subsystems Models
  • 10.
    Problem Analysis: GameYou 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)
  • 11.
    What are theObjects (Classes)? Step #1: Mark nouns 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 !
  • 12.
    What are theOperations/Methods? Step #2: Mark verbs 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!
  • 13.
    Class Diagram 1:Frog Class name Attributes Operations hop () leap () drown () X: integer Y: integer Frog
  • 14.
    Class Diagram 2:Highway Class name Attributes Operations X: integer Y: integer Highway
  • 15.
    Class Diagram 3:Truck Class name Attributes Operations drive () X: integer Y: integer Direction: String Truck
  • 16.
    Define Operation VisibilityOperation visibility is used to enforce Encapsulation Also allows for Information Hiding Visibility may be public , protected , or private Public operations Protected operations Private operations
  • 17.
    How Is VisibilityNoted? The following symbols are used to specify export control: + Public access # Protected access - Private access Class - privateAttribute # protectedAttribute +publicOp() # protectedOp() - privateOp()
  • 18.
    Example: Define OperationsVariable Name : Type Function Return Type Student - name : String - dateOfBirth : Date + canEnroll() : Boolean # hasTakenPrerequisites() : Boolean # hasScheduleConflict() : Boolean
  • 19.
    Class Relationship Is-AAggregation & Composition Aggregation can be shared by others (Uses-A) whereas Composition implies it’s exclusive to it. Link (Association) Relationship which is not Is-A nor Has-A Between classes that exchange messages
  • 20.
    Example: SingleInheritance One class inherits from another Truck tonnage GroundVehicle weight licenseNumber Car owner register( ) getTax( ) Person 0..* Trailer 1 ancestor decendent generalization size
  • 21.
    What is MultipleInheritance? A class can inherit from several other classes Use multiple inheritance only when needed, and always with caution ! Airplane Helicopter Wolf Horse FlyingThing Animal Bird multiple inheritance
  • 22.
    Multiple Inheritance: ProblemsName 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.
    Inheritance vs. AggregationInheritance and aggregation are often confused Inheritance represents an “is-a” or “kind-of” relationship Aggregation represents a “part-of” relationship A WindowWithScrollbar “is a” Window A WindowWithScrollbar “has a” Scrollbar Scrollbar Window WindowWithScrollbar 1 1 Window WindowWithScrollbar Scrollbar
  • 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.
    Generalization to SupportPolymorphism Without Polymorphism With Polymorphism if animal = “Lion” then do the Lion talk else if animal = “Tiger” then do the Tiger talk end do the Animal talk Animal talk () Lion Tiger talk () talk ()

Editor's Notes

  • #17 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.
  • #18 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.
  • #19 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.
  • #20 02/16/11 클래스들 간의 관계는 크게 세가지로 볼 수 있다 . 먼저 계승을 나타내는 IS-A 관계 , member attribute 를 나타내는 aggregation 관계 , 그리고 이 둘이 아닌 일반적인 관계를 들 수 있다 . 이전 단계에서 만든 object diagram 을 보고 이러한 관계성을 찾아내어 클래스 다이어그램에 표시한다 .
  • #21 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.
  • #22 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.
  • #23 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.
  • #24 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
  • #25 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.
  • #26 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.