Inheritance mapping from UML to C#
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Plan of a presentation Introduction Disjoint inheritance Overlapping inhritance Complete inheritance Incomplete inheritance Multi-inheritance Multi-aspect inheritance Dynamic inheritance Tasks 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Introduction Generalization – specialization a relationship that connects so called superclass with another classes that are called subclasses. Person Osoba specjalizacja generalizacja Employee Pracownik Assistant Tutor Reader Professor Asystent Adiunkt Docent Profesor 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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. Inheritance Its 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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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 K1 K2 Student Employee K3 Student_assistant Grid Loop 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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.) 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Disjoint inheritance No common point Default way of implementing For instance: Human Female Male 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Overlapping inheritance Common points Implementing couses change in diagram For instance: Building Garage House {overlapping} 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Overlapping inheritance did with additional class class Building { } class House: Building class Garage: Building class HouseWithGarage: Building Building Garage House House with garage 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Overlapping inheritance did with composition class Building { House house; Garage garage; } class House class Garage Building Garage House 0..1 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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(); 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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) this.salary = salary; Console.WriteLine("I'm an amployee and that's my personall datas:\n" + "Salary: " + this.salary.ToString() + "\n" 2019-02-16 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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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; 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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) this.salary = salary; Console.WriteLine("I'm an amployee and that's my personall datas:\n" + "Salary: " + this.salary.ToString() + "\n" 2019-02-16 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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Multi-inheritance Inheritance from many classes at the same time C# doesn’t support multi-inheritance of classes For instance: Camera Video play() zoom() Picture flash() Photo camera Photo/Video camera Video camera 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-inheritance did with additional subclass class Camera{} interface IPicture { void flash(); void zoom(); } interface IVideo void play(); class VideoCamera: Camera, IVideo public void play(){} public void zoom(){} class PhotoVideoCamera: Camera, IPicture, IVideo public void flash(){} Camera Video play() zoom() Picture flash() Photo camera Photo/Video camera Video camera 2019-02-16 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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritance did with composition Vehicle Drived vehicle Terrain Vehicle drive terrain Wind vehicle Engine vehicle Terrestrial vehicle Water vehicle 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritance did with inheritance and composition Vehicle Drived vehicle Terrain Vehicle drive terrain Wind vehicle Engine vehicle Terrestrial vehicle Water vehicle 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
Multi-aspect inheritance did 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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Dynamic inheritance Object changes its state dynamically Substance Ice Steam Liquid <<dynamic>> 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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(){} 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 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz Tasks 1 A company „Krzak” selling computers needs a 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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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) 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz
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. 2019-02-16 MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz