340 likes | 361 Views
Inheritance. mapping from UML to C#. Plan of a presentation. Introduction Disjoint inheritance Overlapping inhritance Complete inheritance Incomplete inheritance Multi-inheritance Multi-aspect inheritance Dynamic inheritance Tasks. Introduction.
E N D
Inheritance mapping from UML to C#
Plan of a presentation • Introduction • Disjoint inheritance • Overlapping inhritance • Complete inheritance • Incomplete inheritance • Multi-inheritance • Multi-aspect inheritance • Dynamic inheritance • Tasks MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Introduction • Generalization – specializationa relationship that connects so called superclass with another classes that are called subclasses. Osoba Person specjalizacja generalizacja Pracownik Employee Asystent Adiunkt Docent Profesor Assistant Tutor Reader Professor MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Introduction Superclass is more general than its subclasses. Sometimes it is said, that superclass is a generalization of a subclass and subclass is a specialization of a superclass. • InheritanceIts mechanism is strictly related to the specialization – generalization relationship.Thanks to inheritance class invariants are imported from the superclass to the subclasses. It means that subclass has all properties its superclass; moreover subclass can have (and usually has) also additional properties.Invariants’ inheritance is transitive MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Introduction Generalization hierarchy cannot contain a loop. It can be a grid – then it models repeating inheritance (in further part of the presentation) Person K2 K1 Student Employee K3 Student_assistant Grid Loop MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Introduction • The most common mistake is that we make a class hierarchy only based on similarity of the attributes. • When we make a class hierarchy we have to take into account that classes in the hierarchy have to have similar semantics.They also have to have such a property:subclass object is a special case of a superclass object (e.g. Employee is a special case of a Person object.) MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Human Male Female Disjoint inheritance • No common point • Default way of implementingFor instance: MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Building {overlapping} House Garage Overlapping inheritance • Common points • Implementing couses change in diagramFor instance: MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Building House with garage House Garage Overlapping inheritancedid with additional class class Building { } class House: Building { } class Garage: Building { } class HouseWithGarage: Building { } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Building 0..1 0..1 House Garage Overlapping inheritancedid with composition class Building { House house; Garage garage; } class House { } class Garage { } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Complete inheritance • In UML diagram, complete is a default value, so it doesn’t have to be written.It means that all the subclasses were defined (all the subclasses were drawn at the diagram). For instance: Person {abstract} {complete} Student Employee MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
using System; using System.Threading; namespace CompleteInheritance { class CompleteInheritance { static void Main(string[] args) { Student kowalski = new Student("Jan", "Kowalski", "Wrocław", "617"); Employee nowak = new Employee("Jan","Nowak","Warszawa",1000); kowalski.getInfo(); Console.WriteLine("-----------------------------------"); nowak.getInfo(); } } abstract class Person { protected string name; protected string surname; protected string city; public Person(string name, string surname, string city) { this.name = name; this.surname = surname; this.city = city; } public abstract void getInfo(); } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
class Student : Person { string group; public Student(string name, string surname, string city, string group) : base(name, surname, city) { this.group = group; } public override void getInfo() { Console.WriteLine("I'm a student and that's my personall datas:\n" + "Name: " + this.name + "\n" + "Surname: " + this.surname + "\n" + "City: " + this.city + "\n" + "Group: " + this.group + "\n" ); Thread.Sleep(5000); } } class Employee : Person { int salary; public Employee(string name, string surname, string city, int salary) : base(name, surname, city) { this.salary = salary; } public override void getInfo() { Console.WriteLine("I'm an amployee and that's my personall datas:\n" + "Name: " + this.name + "\n" + "Surname: " + this.surname + "\n" + "City: " + this.city + "\n" + "Salary: " + this.salary.ToString() + "\n" ); Thread.Sleep(5000); } } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Incomplete inheritance • In UML diagram, incomplete is not a default value, so it has to be written.It means that not all the subclasses were defined.The superclass here can’t be an abstract class. It is a default inheritance inter alia in C# programming language. For instance: Dog {incomplete} Alsatian Bulldog MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
using System; using System.Threading; namespace IncompleteInheritance { class IncompleteInheritance { static void Main(string[] args) { Student kowalski = new Student("Jan", "Kowalski", "Wrocław", "617"); Employee nowak = new Employee("Jan","Nowak","Warszawa",1000); kowalski.getInfo(); Console.WriteLine("-----------------------------------"); nowak.getInfo(); } } class Person { protected string name; protected string surname; protected string city; public Person(string name, string surname, string city) { this.name = name; this.surname = surname; this.city = city; } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
class Student : Person { string group; public Student(string name, string surname, string city, string group) : base(name, surname, city) { this.group = group; } public void getInfo() { Console.WriteLine("I'm a student and that's my personall datas:\n" + "Name: " + this.name + "\n" + "Surname: " + this.surname + "\n" + "City: " + this.city + "\n" + "Group: " + this.group + "\n" ); Thread.Sleep(5000); } } class Employee : Person { int salary; public Employee(string name, string surname, string city, int salary) : base(name, surname, city) { this.salary = salary; } public void getInfo() { Console.WriteLine("I'm an amployee and that's my personall datas:\n" + "Name: " + this.name + "\n" + "Surname: " + this.surname + "\n" + "City: " + this.city + "\n" + "Salary: " + this.salary.ToString() + "\n" ); Thread.Sleep(5000); } } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Complete and incomplete inheritance • Complete and incomplete inheritances are used mainly in conceptual modelling. They don’t have any reference (odniesienia) in implementation. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Camera Picture flash() zoom() Video play() zoom() Photo camera Photo/Video camera Video camera Multi-inheritance • Inheritance from many classes at the same time • C# doesn’t support multi-inheritance of classesFor instance: MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Camera Picture flash() zoom() Video play() zoom() Photo camera Photo/Video camera Video camera Multi-inheritancedid with additional subclass class Camera{} interface IPicture { void flash(); void zoom(); } interface IVideo { void play(); void zoom(); } class VideoCamera: Camera, IVideo { public void play(){} public void zoom(){} } class PhotoVideoCamera: Camera, IPicture, IVideo { public void flash(){} public void zoom(){} public void play(){} } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritance • Multi-aspect inheritance – inheritance in which there are more than 1 aspect of inheritance. • In this case the aspect of inheritance cannot be ommited on the UML diagram.For instance: Vehicle drive terrain Wind vehicle Engine vehicle Terrestrial vehicle Water vehicle MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritancedid with composition Vehicle Drived vehicle Terrain Vehicle drive terrain Wind vehicle Engine vehicle Terrestrial vehicle Water vehicle MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
using System; namespace MultiAspectInheritance { class MultiAspectInheritance { static void Main(string[] args) { } } class Vehicle { DrivedVehicle drivedVehicle; TerrainVehicle terrainVehicle; } abstract class DrivedVehicle { } abstract class TerrainVehicle { } class WindVehicle : DrivedVehicle { } class EngineVehicle : DrivedVehicle { } class TerrestrialVehicle : TerrainVehicle { } class WaterVehicle : TerrainVehicle { } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritancedid with inheritance and composition Vehicle Drived vehicle Terrain Vehicle drive terrain Wind vehicle Engine vehicle Terrestrial vehicle Water vehicle MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
using System; namespace MultiAspectInheritance { class MultiAspectInheritance { static void Main(string[] args) { } } class Vehicle { TerrainVehicle terrainVehicle; } abstract class DrivedVehicle : Vehicle { } abstract class TerrainVehicle { } class WindVehicle : DrivedVehicle { } class EngineVehicle : DrivedVehicle { } class TerrestrialVehicle : TerrainVehicle { } class WaterVehicle : TerrainVehicle { } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritancedid with encapsulated specification Vehicle drive Wind vehicle Engine vehicle terrain terrain Terrestrial and wind vehicle Water and wind vehicle Terrestrial and engine vehicle Water and engine vehicle MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
using System; namespace MultiAspectInheritance { class MultiAspectInheritance { static void Main(string[] args) { } } abstract class Vehicle { } abstract class WindVehicle : Vehicle { } abstract class EngineVehicle : Vehicle { } class TerrestrialAndWindVehicle : Vehicle { } class WaterAndWindVehicle : Vehicle { } class TerrestrialAndEngineVehicle : Vehicle { } class WaterAndEngineVehicle : Vehicle { } } MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Substance <<dynamic>> Liquid Steam Ice Dynamic inheritance • Object changes its state dynamically MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Dynamic inheritance interface ISubstance { void changeState(); } class Liquid: ISubstance { public Liquid(){} public void changeState() { Console.WriteLine(„Liquid -> Ice"); } } class Ice: ISubstance { public Ice(){} public void changeState() { Console.WriteLine(„Ice -> Liquid"); } } Ice ice = new Ice(); Liquid liquid = new Liquid(); ISubstance h2o = ice; h2o.changeState(); Output: Liquid -> Ice h2o = liquid; h2o.changeState(); Output: Ice -> Liquid MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Tasks 1 A company „Krzak” selling computers needsa system that will store information about computers and palmtops. The „Krzak” sells coputers such as notebooks and desktop computers as well as such types of palmtops: with colour and monochromatic display. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Task 2 A company „Krzak” selling computers needs a system that will store information about computers. The „Krzak” sells computers such as notebooks, desktop computers etc. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Task 3 Car dealer „Krzak” selling cars wants to store information about available cars. The cars are divided with respect to engine (petrol, diesel) as well as equipment (air condition, audio system) MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Task 4 Company „Posprzątamy” needs a system that will store information about cleaners and owners of company. Remember that owner cannot work as a cleaner. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Task 5 Computer company selling network devices needs a system that will store information about routers, modems and network cards. Company sells also routers with bult-in modems. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Task 6 Real estate agency „Dom” needs a system that will store information about houses they have in offer. House can be empty, rented, sold or in renovation. MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz