Language: Use Scoping-Overriding-Overloading
Shows how to allow various levels of access to the members of a class,
including Public, Private, Protected and otherwise. To demonstrate how to
extend derived classes with features like Overloading and Overriding.
Featured Highlights:
This application simulates a simple hiring system, allowing for the
hiring of full-time, part-time and temporary employees. Each kind of employee
has specific features that set them apart from the other kinds. For
example, only full-time employees get annual leave, and only temporary
employees have an expected termination date when they are hired. Part-time
employees are required to work at least 20 hours per week. However, all
employees have many things in common: they all get hired, all have salaries,
each has a name, etc.
The application utilizes a base class called Employee, from which the classes
FullTimeEmployee, PartTimeEmployee and TempEmployee are derived. Each derived
class extends the base class in some way: by overriding methods of the base
class, by implementing new methods or properties of its own, or by replacing
(shadowing) members of the base class. There is also a Friend class called
EmployeeDataManager which simulates reading and writing employee data to and
from a database.
The application demonstrates the use of these statements and modifiers in
classes and their members:
-
Inherits:
Specifies the class (also known as the base class) from which the current class
inherits.
-
NotInheritable:
Prevents programmers from using the class as a base class.
-
MustInherit:
Specifies that instances of the given class cannot be created. The only way to
use the class is to inherit from it.
-
Overridable:
Allows a property or a method to be overridden in an inheriting class. Public
methods are NotOverridable by default.
-
Overrides:
Allows you to override a property or method that is defined in the base class.
-
NotOverridable (default):
Prevents a property or method from being overridden in an inheriting class.
-
MustOverride:
Requires the inheriting class to override the property or method.
-
Shadows: Allows reuse of the names of inherited class members,
making all of the inherited type members unavailable in the derived
class.
It also demonstrates scoping with the use of these keywords:
-
Public:
Classes and procedures declared with the Public keyword have public access.
There are no restrictions on their accessibility.
-
Protected:
Class members declared with the Protected keyword are accessible only from
within their own class or from a derived class.
-
Friend:
Classes and procedures declared with the Friend keyword are accessible from
within the program where they are declared and from anywhere else in the same
assembly.
-
Private:
Class members declared with the Private keyword are accessible only from within
the class where they are declared.
-
Shared: Procedures declared with the Shared keyword can be
used without necessarily having to create an instance of the class they belong
to. You can call a shared procedure either by qualifying it with the class name
(EmployeeDataManager.WriteEmployeeData), or with the variable name of a
specific instance of the class (edmManager.WriteEmployeeData).
Requirements:
-
There is no database access, since this is a simulation. All data is in memory.
-
Set breakpoints at strategic places throughout the classes to observe the
interaction between base class and inherited class.
Running the Sample:
Simply press F5.
See Also