Abstract


Abstract classes are concepts. Object-oriented applications model the real world, which is replete with concepts. For developers of a graphics program, a geometric shape is a concept. Have you even seen a geometric shape? No. Rather, you have seen types of geometric shapes, such as triangles, ellipses, rectangles, and lines. Geometric shape is a description of a category—a kind of shape. Conversely, a rectangle is tangible: A television, a box, and this book are actual rectangles.

The Employee class is an abstract class. Why? In the personnel domain, employee is a categorization of employees. Each employee is specifically an hourly, salary, or commissioned employee. How an employee is paid is determined by the type of employee. Payroll calculations are conducted into the derived type, such as HourlyEmployee. The Employee base type is a container of general information such as the employee name. However, without pay specifics, the Employee class is incomplete. Most employees value getting paid. For that reason, Employee is a concept and abstract.

The abstract keyword makes a class abstract. Abstract classes exist extensively for inheritance. You cannot create an instance of an abstract class. Nonabstract classes are concrete classes. You can create an instance of a concrete class. Static classes, value types, and interfaces do not support the abstract modifier.

In the next example, Employee is the base class and is abstract. HourlyEmployee is the derived class and is concrete.

 public class Starter{     public static void Main(){         Employee obj1=new Employee();              // Not valid         HourlyEmployee obj2=new HourlyEmployee();  // Valid     } } public abstract class Employee { // abstract } public class HourlyEmployee: Employee {  // concrete } 

Members can also be abstract. Methods, properties, indexers, and event members can be abstract. However, static members cannot be abstract. In addition, classes with abstract members must also be abstract. An abstract member has a signature but no function body. Virtual is implied with abstract functions. Abstract members must be implemented in the derived type.

In the real world, a parent can ask a child to do something with or without instructions. The parent can provide instruction to the child to complete the request. Alternatively, a parent might provide no directions. The child must accomplish the task. However, the parent does not care about the details of the implementation. An abstract function is an example of the latter. Abstract methods are a means of assuring that derived types implement required methods. The derived type inherits no implementation (instructions) from the base type. Derived types must override all abstract functions. If not, the derived type is incomplete and in error.

In the following code, Employee mandates that derived types implement the CalculatePay method. Because the class has an abstract method, the Employee class is also abstract:

 public abstract class Employee {     public virtual void Pay() {     }     public abstract void CalculatePay(); } public class HourlyEmployee: Employee {     public override void Pay() {         CalculatePay();     }     public override void CalculatePay() {     } } 

For completeness, here is an example of an abstract property:

 public abstract class ZClass {     public abstract int PropA {         get;         set;     } } public class YClass: ZClass {     public override int PropA {         get {            return 0;         }         set {         }     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net