1 / 34

Inheritance

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.

tmiriam
Télécharger la présentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance mapping from UML to C#

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Human Male Female Disjoint inheritance • No common point • Default way of implementingFor instance: MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz

  8. Building {overlapping} House Garage Overlapping inheritance • Common points • Implementing couses change in diagramFor instance: MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. Substance <<dynamic>> Liquid Steam Ice Dynamic inheritance • Object changes its state dynamically MAS 2004 Wojciech Gorzkowski & Michał Juchnowicz

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

More Related