Object Oriented Design Lecture #4 – Inheritance, Interfaces, Classes Tomasz Pieciukiewicz
Agenda Inheritance Parametrized Classes Interfaces Attributes and Methods
INHERITANCE
Inheritance types disjont (default) overlapping complete incomplete (default)... (ellipsis)
Inheritance types Do not put default inheritance information on the diagram Tree OakBirchPine {disjoint, complete} species
Single-aspect inheritance Exchange area Pipe diameter Tank volume pressure Equipment type Pump type Tank type Specialization aspect Equipment name manufacturer cost Suction pressure Compression pressure flow Pump Heat exchanger
Single-aspect inheritance Equipment name manufacturer cost Suction pressure Compression pressure flow Exchange area Pipe diameter volume pressure Equipment type Suction pressure Compression pressure flow Exchange area Pipe diameter Tank volume pressure Equipment type Equipment name manufacturer cost PumpHeat Exchanger
Multi-aspect inheritance Vehicle {overlapping} Wind-powered Vehicle Engine-powered Vehicle Land Vehicle Water Vehicle propulsion terrain {overlapping} Two inheritance aspects: terrain and propulsion
Multi-inheritance Name Person Employed Student Employee Salary Student ID
Multi-inheritance: problems Vehicle {economy speed is 50% top speed} top_speed economy_speed() AmphibianCarYacht economy_speed() Water vehicleLand vehicle Name conflict: which top_speed attribute should be inherited by the amphibian? Does the economy_speed() method meaning depend on the inheritance path? (O2: mechanism for renaming of inherited properties; Eiffel: conflict treated as an error.)
Dynamic Inheritance Person Manager Engineer Salesman Female Male {mandatory} sex « dynamic » profession
Abstract Classes Abstract class can not have instances Defines common part of group of classes with similar semantics In UML marked with tagged value {abstract = TRUE} (=TRUE may be omitted) OR by writing the entity’s (class or method) name in italics. Concrete class – can have instances.
Abstract Classes Persona {abstract} Person Company Sequence first next Int sequence... implementation Char sequence... implementation
Abstract Methods Per-Hour Employee perHour holidaysHour noofHours noofHolidayHours calculateSalary Employee weeklySalary numberofweeks calculateSalary Contract Employee monthlySalary calculateSalary calculateSalary{abstract} Employee {abstract}
Abstract Methods Abstract method – specified in abstract class, implemented in subclasses Abstract class may have abstract methods, but does not have to Concrete class has to implement all inherited abstract methods not implemented in any superclass Concrete class can’t have abstract methods
PARAMETRIZED CLASSES
Parametrized Classes - notation Set Current parametrization parameter Set insert(T) delete(T) T EmployeeSet « bind » template
INTERFACES
Interfaces dependency Company realization Person {abstract} First name Last name Date of birth Calculate age Employee salary position Change salary IEmployee «interface» + Change salary
Interfaces Stereotype > Method specification, no implementation No attributes All methods are public Method implementation in class which realizes the interface Dependency – shows classes that use an interface
Interfaces Employee IEmployee Person Company The abstract class and interface were shown as interface definitions. Abstract classes – unlike interfaces – may contain attributes and method implementations
CLASS EXTENT
Class Extent Current set of all class instances. Implemented as a special data structure attached to the class. Some methods work on instances Some methods work on the class extent
Class Extent I set of direct (no sub-classes) instances of the given class II set of instances of the given class (direct and indirect), limited to the attributes specified in the given class III set of instances of the given class (direct and indirect)
ATTRIBUTES AND METHODS
Attributes Simple Complex Optional [0..1] Repeatable [x..y] Derived (/name) Class attributes Object attributes
Methods abstract object class methods
Method Overriding Method specification from specialized class overrides specification from general class Related with method polymorphism Requires dynamic binding Supports reuse Employee name... fire()... Manager fire()
Binding Process of replacing symbolic identifier (name) for value, address or internal entity identifier May be early or late
Early vs Late Binding Early (static) binding: during compilation and consolidation Pros: ◦ better performance, ◦ full static typechecking Cons: ◦ less flexible, ◦ No dynamic app extensions (plug-ins) Late (dynamic) binding: at runtime Pros: ◦ Method overriding ◦ Dynamic app extension (plug-ins) ◦ Rapid development Cons: ◦ Worse performance ◦ Difficult typechecking
Late Binding – why bother? Polymorphism Dynamic views Dynamic stored procedures Query languages Object migration Database schema evolution
Method overloading Symbol’s semantics depend on the context – e.g. number or type of arguments ◦ move (x,y) and move (x,y,z) ◦ move (int, int) and move (float, float) Some authors (e.g. Cardelli – evangelist of polymorphic types theory) believe that overloading is not a form of polymorphism ◦ All methods implementing an operation should have the same signature
QUESTIONS?