120 likes | 276 Views
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.
E N D
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 Properties Methods Methods Methods Properties Derived Class Properties Child Class, Subclass Methods
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
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
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 + “ ) ” } }
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 + “ ) ” } }
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
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”));
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);
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