/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate protected. using System; class B { protected int i, j; // private to B, but accessible by D public void set(int a, int b) { i = a; j = b; } public void show() { Console.WriteLine(i + " " + j); } } class D : B { int k; // private // D can access B's i and j public void setk() { k = i * j; } public void showk() { Console.WriteLine(k); } } public class ProtectedDemo { public static void Main() { D ob = new D(); ob.set(2, 3); // OK, known to D ob.show(); // OK, known to D ob.setk(); // OK, part of D ob.showk(); // OK, part of D } }
Class Interface
Member Hiding
using System; public class MotorVehicle { public string make; public string model; public MotorVehicle(string make, string model) { Console.WriteLine("In MotorVehicle constructor"); this.make = make; this.model = model; Console.WriteLine("this.make = " + this.make); Console.WriteLine("this.model = " + this.model); } public void DisplayModel() { Console.WriteLine("In MotorVehicle DisplayModel() method"); Console.WriteLine("model = " + model); } } public class Product : MotorVehicle { public new string model; public Product(string make, string model) : base(make, "Test") { Console.WriteLine("In Product constructor"); this.model = model; Console.WriteLine("this.model = " + this.model); } public new void DisplayModel() { Console.WriteLine("In Product DisplayModel() method"); Console.WriteLine("model = " + model); base.DisplayModel(); } } class MainClass { public static void Main() { Console.WriteLine("Creating a Product object"); Product myProduct = new Product("Toyota", "MR2"); Console.WriteLine("Back in Main() method"); Console.WriteLine("myProduct.make = " + myProduct.make); Console.WriteLine("myProduct.model = " + myProduct.model); Console.WriteLine("Calling myProduct.DisplayModel()"); myProduct.DisplayModel(); } }
Member Accessibility
using System; public class MotorVehicle { private string make; protected string model; public MotorVehicle(string make, string model) { this.make = make; this.model = model; } public virtual void Start() { TurnStarterMotor(); System.Console.WriteLine("Vehicle started"); } private void TurnStarterMotor() { System.Console.WriteLine("Turning starter motor..."); } } public class Product : MotorVehicle { public Product(string make, string model) : base(make, model) { // do nothing } public override void Start() { Console.WriteLine("Starting " + model); base.Start(); } } class MainClass { public static void Main() { Product myProduct = new Product("Toyota", "MR2"); myProduct.Start(); } }
Using Access Modifiers
public class Product { public string make; protected internal string model; internal string color; protected int horsepower = 150; private int yearBuilt; public void SetYearBuilt(int yearBuilt) { this.yearBuilt = yearBuilt; } public int GetYearBuilt() { return yearBuilt; } public void Start() { System.Console.WriteLine("Starting Product ..."); TurnStarterMotor(); System.Console.WriteLine("Product started"); } private void TurnStarterMotor() { System.Console.WriteLine("Turning starter motor ..."); } } class MainClass { public static void Main() { Product myProduct = new Product(); myProduct.make = "Toyota"; myProduct.model = "MR2"; myProduct.color = "black"; myProduct.SetYearBuilt(1995); System.Console.WriteLine("myProduct.make = " + myProduct.make); System.Console.WriteLine("myProduct.model = " + myProduct.model); System.Console.WriteLine("myProduct.color = " + myProduct.color); System.Console.WriteLine("myProduct.GetYearBuilt() = " + myProduct.GetYearBuilt()); myProduct.Start(); } }
Create an abstract class
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Create an abstract class.
using System;
abstract class TwoDShape {
double pri_width; // private
double pri_height; // private
string pri_name; // private
// A default constructor.
public TwoDShape() {
width = height = 0.0;
name = “null”;
}
// Parameterized constructor.
public TwoDShape(double w, double h, string n) {
width = w;
height = h;
name = n;
}
// Construct object with equal width and height.
public TwoDShape(double x, string n) {
width = height = x;
name = n;
}
// Construct an object from an object.
public TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
}
// Properties for width, height, and name
public double width {
get { return pri_width; }
set { pri_width = value; }
}
public double height {
get { return pri_height; }
set { pri_height = value; }
}
public string name {
get { return pri_name; }
set { pri_name = value; }
}
public void showDim() {
Console.WriteLine(“Width and height are ” +
width + ” and ” + height);
}
// Now, area() is abstract.
public abstract double area();
}
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape {
string style; // private
// A default constructor.
public Triangle() {
style = “null”;
}
// Constructor for Triangle.
public Triangle(string s, double w, double h) :
base(w, h, “triangle”) {
style = s;
}
// Construct an isosceles triangle.
public Triangle(double x) : base(x, “triangle”) {
style = “isosceles”;
}
// Construct an object from an object.
public Triangle(Triangle ob) : base(ob) {
style = ob.style;
}
// Override area() for Triangle.
public override double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine(“Triangle is ” + style);
}
}
// A derived class of TwoDShape for rectangles.
class Rectangle : TwoDShape {
// Constructor for Rectangle.
public Rectangle(double w, double h) :
base(w, h, “rectangle”){ }
// Construct a square.
public Rectangle(double x) :
base(x, “rectangle”) { }
// Construct an object from an object.
public Rectangle(Rectangle ob) : base(ob) { }
// Return true if the rectangle is square.
public bool isSquare() {
if(width == height) return true;
return false;
}
// Override area() for Rectangle.
public override double area() {
return width * height;
}
}
public class AbsShape {
public static void Main() {
TwoDShape[] shapes = new TwoDShape[4];
shapes[0] = new Triangle(“right”, 8.0, 12.0);
shapes[1] = new Rectangle(10);
shapes[2] = new Rectangle(10, 4);
shapes[3] = new Triangle(7.0);
for(int i=0; i < shapes.Length; i++) { Console.WriteLine("object is " + shapes[i].name); Console.WriteLine("Area is " + shapes[i].area()); Console.WriteLine(); } } } [/csharp]
Demostrates the use of an abstract class, including an abstract method and abstract properties
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Abstract.cs -- Demostrates the use of an abstract class, including // an abstract method and abstract properties. // // Compile this program with the following command line: // C:>csc Abstract.cs // namespace nsAbstract { using System; public class AbstractclsMain { static public void Main () { // Create an instance of the derived class. clsDerived derived = new clsDerived (3.14159); // Calling GetAbstract() actually calls the public method in the // base class. There is no GetAbstract() in the derived class. derived.GetAbstract(); } } // Declare an abstract class abstract class clsBase { // Declare an abstract method. Note the semicolon to end the declaration abstract public void Describe(); // Declare an abstract property that has only a get accessor. // Note that you // do not prove the braces for the accessor abstract public double DoubleProp { get; } // Declare an abstract property that has only a set accessor. abstract public int IntProp { set; } // Declare an abstract propety that has both get and set accessors. Note // that neither the get or set accessor may have a body. abstract public string StringProp { get; set; } // Declare a method that will access the abstract members. public void GetAbstract () { // Get the DoubleProp, which will be in the derived class. Console.WriteLine ("DoubleProp = " + DoubleProp); // You can only set the IntProp value. The storage is in the // derived class. IntProp = 42; // Set the StringProp value StringProp = "StringProperty actually is stored in " + "the derived class."; // Now show StringProp Console.WriteLine (StringProp); // Finally, call the abstract method Describe (); } } // Derive a class from clsBase. You must implement the abstract members class clsDerived : clsBase { // Declare a constructor to set the DoubleProp member public clsDerived (double val) { m_Double = val; } // When you implement an abstract member in a derived class, you may not // change the type or access level. override public void Describe() { Console.WriteLine ("You called Describe() from the base " + "class but the code body is in the " + "derived class"); Console.WriteLine ("m_Int = " + m_Int); } // Implement the DoubleProp property. This is where you provide a body // for the accessors. override public double DoubleProp { get {return (m_Double);} } // Implement the set accessor for IntProp. override public int IntProp { set {m_Int = value;} } // Implement StringProp, providing a body for both the get // and set accessors. override public string StringProp { get {return (m_String);} set {m_String = value;} } // Declare fields to support the properties. private double m_Double; private int m_Int; private string m_String; } }
Illustrates abstract classes and methods
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example7_9.cs illustrates abstract classes and methods */ using System; // declare the abstract MotorVehicle class abstract class MotorVehicle { // declare the fields public string make; public string model; // define a constructor public MotorVehicle(string make, string model) { this.make = make; this.model = model; } // declare the abstract Accelerate() method (no code) abstract public void Accelerate(); } // declare the Car class (derived from MotorVehicle) class Car : MotorVehicle { // define a constructor public Car(string make, string model) : base(make, model) { // do nothing } // override the Accelerate() method (contains code) public override void Accelerate() { Console.WriteLine("In Car Accelerate() method"); Console.WriteLine(model + " accelerating"); } } public class Example7_9 { public static void Main() { // create a Car object Console.WriteLine("Creating a Car object"); Car myCar = new Car("Toyota", "MR2"); // call the Car object's Accelerate() method Console.WriteLine("Calling myCar.Accelerate()"); myCar.Accelerate(); } }