sealed
prevents inheritance from occurring
// The MiniVan class cannot be extended! sealed class MiniVan : Car { }Initialize Base Class
// As a general rule, all subclasses should explicitly call an appropriate base class constructor. public SalesPerson(string fullName, int age, int empID, float currPay, string ssn, int numbOfSales) : base(fullName, age, empID, currPay, ssn) { // This belongs with us! SalesNumber = numbOfSales; }Delegation Delegation is simply the act of adding public members to the containing class that make use of the contained object’s functionality
//Declare Employee with BenefitPackage public partial class Employee { // Contain a BenefitPackage object. protected BenefitPackage empBenefits = new BenefitPackage(); // Expose certain benefit behaviors of object. public double GetBenefitCost() { return empBenefits.ComputePayDeduction(); } // Expose object through a custom property. public BenefitPackage Benefits { get { return empBenefits; } set { empBenefits = value; } } ... }Using Delegation class
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); double cost = chucky.GetBenefitCost(); Console.ReadLine(); }Nested Class
- Nested types allow you to gain complete control over the access level of the inner type, as they may be declared privately (recall that non-nested classes cannot be declared using the private keyword).
- Because a nested type is a member of the containing class, it can access private members of the containing class
- Oftentimes, a nested type is only useful as a helper for the outer class, and is not intended for use by the outside world
Example public class OuterClass { // A public nested type can be used by anybody. public class PublicInnerClass {} // A private nested type can only be used by members of the containing class. private class PrivateInnerClass {} }
Scope of inner class static void Main(string[] args) { // Create and use the public inner class. OK! OuterClass.PublicInnerClass inner; inner = new OuterClass.PublicInnerClass(); // Compiler Error! Cannot access the private class. OuterClass.PrivateInnerClass inner2; inner2 = new OuterClass.PrivateInnerClass(); }
nested class partial class Employee ***Partial keyword*** { public class BenefitPackage ***nested class*** { // Assume we have other members that represent // dental/health benefits, and so on. public double ComputePayDeduction() { return 125.0; } } ... }