/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A multilevel hierarchy. using System; class TwoDShape { double pri_width; // private double pri_height; // private // Default constructor. public TwoDShape() { width = height = 0.0; } // Constructor for TwoDShape. public TwoDShape(double w, double h) { width = w; height = h; } // Construct object with equal width and height. public TwoDShape(double x) { width = height = x; } // Properties for width and height. public double width { get { return pri_width; } set { pri_width = value; } } public double height { get { return pri_height; } set { pri_height = value; } } public void showDim() { Console.WriteLine("Width and height are " + width + " and " + height); } } // A derived class of TwoDShape for triangles. class Triangle : TwoDShape { string style; // private /* A default constructor. This invokes the default constructor of TwoDShape. */ public Triangle() { style = "null"; } // Constructor public Triangle(string s, double w, double h) : base(w, h) { style = s; } // Construct an isosceles triangle. public Triangle(double x) : base(x) { style = "isosceles"; } // Return area of triangle. public double area() { return width * height / 2; } // Display a triangle's style. public void showStyle() { Console.WriteLine("Triangle is " + style); } } // Extend Triangle. class ColorTriangle : Triangle { string color; public ColorTriangle(string c, string s, double w, double h) : base(s, w, h) { color = c; } // Display the color. public void showColor() { Console.WriteLine("Color is " + color); } } public class Shapes6 { public static void Main() { ColorTriangle t1 = new ColorTriangle("Blue", "right", 8.0, 12.0); ColorTriangle t2 = new ColorTriangle("Red", "isosceles", 2.0, 2.0); Console.WriteLine("Info for t1: "); t1.showStyle(); t1.showDim(); t1.showColor(); Console.WriteLine("Area is " + t1.area()); Console.WriteLine(); Console.WriteLine("Info for t2: "); t2.showStyle(); t2.showDim(); t2.showColor(); Console.WriteLine("Area is " + t2.area()); } }
Call a hidden method
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Call a hidden method. using System; class A { public int i = 0; // show() in A public void show() { Console.WriteLine("i in base class: " + i); } } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int a, int b) { base.i = a; // this uncovers the i in A i = b; // i in B } // This hides show() in A. Notice the use of new. new public void show() { base.show(); // this calls show() in A // this displays the i in B Console.WriteLine("i in derived class: " + i); } } public class UncoverName123 { public static void Main() { B ob = new B(1, 2); ob.show(); } }
Using base to overcome name hiding
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Using base to overcome name hiding. using System; class A { public int i = 0; } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int a, int b) { base.i = a; // this uncovers the i in A i = b; // i in B } public void show() { // this displays the i in A. Console.WriteLine("i in base class: " + base.i); // this displays the i in B Console.WriteLine("i in derived class: " + i); } } public class UncoverName1231 { public static void Main() { B ob = new B(1, 2); ob.show(); } }
An example of inheritance-related name hiding
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // An example of inheritance-related name hiding. using System; class A { public int i = 0; } // Create a derived class. class B : A { new int i; // this i hides the i in A public B(int b) { i = b; // i in B } public void show() { Console.WriteLine("i in derived class: " + i); } } public class NameHiding { public static void Main() { B ob = new B(2); ob.show(); } }
Four layers of class hierarchy
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class MyMainClass3 { static void Main(string[] args) { //The function called is based //upon the type called by new. A MyA = new D(); B MyB = new C(); MyA.Display(); //Calls D Display MyB.Display(); //Calls C Display // followed by B's Display //via the base keyword } } class A { public virtual void Display() { Console.WriteLine("Class A's Display Method"); } } class B: A { public override void Display() { Console.WriteLine("Class B's Display Method"); } } class C: B { public override void Display() { Console.WriteLine("Class C's Display Method"); base.Display(); } } class D: C { public override void Display() { Console.WriteLine("Class D's Display Method"); } } }
Inheritance 3
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class InheritanceChapter_5___Building_Your_Own_Classes { static void Main(string[] args) { B MyB = new D(); D MyD = new D(); //Both result in in D's instance of Display being //called MyB.Display(); MyD.Display(); } } public class B { public virtual void Display() { Console.WriteLine("Class B's Display Method"); } } public class C: B { public override void Display() { Console.WriteLine("Class C's Display Method"); } } public class ContainedClass { int MyInt = 0; } public class D: C { public ContainedClass MyClass = new ContainedClass(); public override void Display() { Console.WriteLine("Class D's Display Method"); } } }
Demonstrates deriving a new class from a base class in another assembly
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Access1.cs - demonstrates deriving a new class from a base class in // another assembly. Also demonstrates how a derived class // may provide a public property to expose a protected member // of a base class. // // Compile this program with the following command line: // C:>csc /r:access.exe Access1.cs // namespace nsAccess { using System; public class Access1 { static public void Main () { clsDerived derived = new clsDerived (); derived.AccessIt = 42; derived.ShowField (); } } // // Derive a class from the base class and give it a public // property to access the private field in the base class class clsDerived : clsBase { public int AccessIt { get {return (Private);} set {Private = value;} } } public class clsBase { private int m_Private; protected int Private { get {return (m_Private);} set {m_Private = value;} } public void ShowField () { Console.WriteLine ("The value of private field m_Private is " + m_Private); } } }