1 / 11

Inheritance

Inheritance. The Concept of Inheritance Related access modifiers Override - Virtual Hide - New Pholymorphism Casting. What is inheritance?. Parent Class, Superclass. Base Class. Properties. Derived Class. Properties. Methods. Properties. Properties. Inherited. Methods.

ansel
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

  2. The Concept of Inheritance • Related access modifiers • Override - Virtual • Hide - New • Pholymorphism • Casting

  3. What is inheritance? Parent Class, Superclass Base Class Properties Derived Class Properties Methods Properties Properties Inherited Methods Properties Methods Methods Methods Properties Derived Class Properties Child Class, Subclass Methods

  4. Derived Classes from Base Class Product string Code string Description decimal Price string GetDisplayText (string) Book Software string Code string Description decimal Price string GetDisplayText (string) string Author string Code; string Description; decimal Price; string GetDisplayText (string) string Version

  5. Protected Internal Internal Protected Access Modifiers • public vs. private • protected: available only to the current class or to derived class • internal: available only to classes in the current assembly • protected internal: available only to the current classes, derived classes, or classes in the current assembly • NOTE: Default access is private Assembly 1 Assembly 2 Assembly 3

  6. Override Product public class Product { public string Code; public string Description; public decimal Price; public virtual string GetDisplayText (string sep) { return Code + sep + Description + sep + Price.ToString (“C”); } } Book public class Book : Product { public string Author; public Book (string code, string description, string author, decimal price) : base(code, description, price) { this.Author = author; } public override string GetDisplayText (string sep) { return this.Code + sep + this.Description + “ ( ” + this.Author + “ ) ” + sep + this.Price.ToString (“C”); // or return base.GetDisplayText(sep) + “ ( ” + this.Author + “ ) ” } }

  7. Hiding Product public class Product { public string Code; public string Description; public decimal Price; public string GetDisplayText (string sep) { return Code + sep + Description + sep + Price.ToString (“C”); } } Book public class Book : Product { public string Author; public Book (string code, string description, string author, decimal price) : base(code, description, price) { this.Author = author; } public new string GetDisplayText (string sep) { return this.Code + sep + this.Description + “ ( ” + this.Author + “ ) ” + sep + this.Price.ToString (“C”); // or return base.GetDisplayText(sep) + “ ( ” + this.Author + “ ) ” } }

  8. Overriding vs. Hiding Base Class Derived Class Properties Properties Overriding Methods Methods Polymorphism is provided Virtual Override Base Class Derived Class Properties Properties Hiding Methods Methods Polymorphism is not provided New

  9. Polymorphism • Feature of inheritance that lets you treat objects of different subclasses that are derived from the same base class as if they had the type of the base class • Book b = new Book(“MIS2010”, “Jin’s MIS 2010”, “Jinyoung Min”, 54.50m); • Software s = new Software(“NPTK”, “.NET Programmer’s Toolkit”, “2.1”, 149.50m); • Product p; • p = b; • MessaageBox.Show(p.GetDisplayText(“\n”)); • p = s; • MessaageBox.Show(p.GetDisplayText(“\n”));

  10. Casting • Public void DisplayProduct (Product p) • { • MessageBox.Show (p.GetDisplayText(); • } • Public void DisplayBook (Book b) • { • MessageBox.Show(b.GetDisplayText(); • } • Book b = new Book(“MIS2010”, “Jin’s MIS 2010”, “Jinyoung Min”, 54.50m); • DisplayProduct(b); • Product p = new Book(“MIS2010”, “Jin’s MIS 2010”, “Jinyoung Min”, 54.50m); • DisplayBook((Book)p); • Product p = new Software(“MIS2010”, “Jin’s MIS 2010”, “9.0”, 99.50m); • DisplayBook((Book)p);

  11. Exercise • Debug an example • Make one more category of the product, the “Audio” • - Create Audio Class that inherits Product class which works just like Book or Software Class • - Add radio button “Audio” • - Then the text of the label for “Author” has to change to “Voice”, when Audio button is clicked

More Related