You are on page 1of 5

7.27.1.

Using Abstract Classes


1. 2. 3. 4. 5. 6. 7. 8. 9.

An abstract method is created by specifying the abstract type modifier. An abstract method contains no body. An abstract method is not implemented by the base class. An abstract method is automatically virtual. A derived class must override it. The abstract modifier cannot be applied to static methods. Properties can also be abstract. A class containing abstract methods must be declared as abstract with the abstract specifier There can be no objects of an abstract class.

10. If a derived class doesn't implement all of the abstract methods in the base class, then the de must also be specified as abstract. To declare an abstract method, use this general form:
abstract type name(parameter-list);

Introduction
A detailed analysis of Abstract classes and methods in C# with some concrete examples. The keyword abstract can be used with both classes and methods in C# to declare them as abstract. The classes, which we can't initialize, are known as abstract classes. They provide only partial implementations. But another class can inherit from an abstract class and can create their instances. You need to mark class members that have no implementation with the abstract modifier. ou also need to label a class containing such members as abstract. A class with abstract members cannot be instantiated with the new operator. You can derive both abstract and non-abstract classes from an abstract base class. Interfaces are implicitly abstract. They cannot be instantiated, and must be implemented by a non-abstract class. Therefore, you cannot mark interfaces and their members as abstract. You may not combine the abstract modifier with the other inheritance modifier, final. You may not combine either of these inheritance modifiers (abstract and final) with the static modifier. See the below examples, Example 1

Collapse | Copy Code

namespace Abstract { /// <summary> /// an abstract class with a non-abstract method /// </summary> abstract class MyAbs { public void NonAbMethod() { Console.WriteLine("Non-Abstract Method"); } } class MyClass : MyAbs { } class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { //MyAbs mb = new MyAbs();//not possible to create an instance MyClass mc = new MyClass(); mc.NonAbMethod(); // Displays 'Non-Abstract Method' } } }
Collapse | Copy Code

namespace Abstract2 { /// <summary> /// An abstract method is a method without any method body. /// They are implicitly virtual in C#. /// </summary> abstract class MyAbs {

public void NonAbMethod() { Console.WriteLine("Non-Abstract Method"); } public abstract void AbMethod(); // An abstract method } class MyClass : MyAbs//must implement base class abstract methods { public override void AbMethod() { Console.WriteLine("Abstarct method"); } } class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { MyClass mc = new MyClass(); mc.NonAbMethod(); mc.AbMethod(); } } }

7.27.5.Define abstract class and abstract method


using System; abstract class Employee { public Employee(string name, float billingRate) { this.name = name; this.billingRate = billingRate; } virtual public float CalculateCharge(float hours) { return(hours * billingRate); }

abstract public string TypeName(); private string name; protected float billingRate; } class Manager: Employee { public Manager(string name, float billingRate) : base(name, billingRate) { } override public float CalculateCharge(float hours) { if (hours < 1.0F) hours = 1.0F; // minimum charge. return(hours * billingRate); } // This override is required, or an error is generated. override public string TypeName() { return("Manager"); } } class Clerk: Employee { public Clerk(string name, float billingRate) : base(name, billingRate) { } override public string TypeName() { return("Clerk"); }

} class Test { public static void Main() { Employee[] earray = new Employee[2]; earray[0] = new Manager("A", 40.0F); earray[1] = new Clerk("C", 45.0F); Console.WriteLine("{0} charge = {1}", earray[0].TypeName(), earray[0].CalculateCharge(2F)); Console.WriteLine("{0} charge = {1}", earray[1].TypeName(), earray[1].CalculateCharge(0.75F)); } }

Manager charge = 80

Clerk charge = 33.75

You might also like