qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvb Abstract Classes nmqwertyasdfghjklzcvbnmqwertyuio .OOPs pasdfghjklzxcvbnmqwertyuiopasdfghj klzxcvbnmqwertyuiopasdfghjklzxcvbn mqwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopasdfghjkl zxcvbnmqwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnmrtyuiopa sdfghjklzxcvbnmqwertyuiopasdfghjklz xcvbnmqwertyuiopasdfghjklzxcvbnmq Document Prepared by : Sehul Soni Call : +91-9925025622 Email : sehulsoni@gmail.com Chat : sehulsoni@yahoo.com Talk : sehulsoni1011 (Skype) Abstract Classes Introduction They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code. An abstract class is denoted in Visual Basic by the keyword MustInherit. In C#, the abstract modifier is used. Any methods that are meant to be invariant may be coded into the base class, but any methods that are to be implemented are marked in Visual Basic with the MustOverride modifier. In C#, the methods are marked abstract. The following example shows an abstract class: ' Visual Basic Public MustInherit Class WashingMachine Sub New() ' Code to instantiate the class goes here. End sub Public MustOverride Sub Wash Public MustOverride Sub Rinse (loadSize as Integer) Public MustOverride Function Spin (speed as Integer) as Long End Class // C# abstract class WashingMachine { public WashingMachine() { // Code to initialize the class goes here. } abstract public void Wash(); abstract public void Rinse(int loadSize); abstract public long Spin(int speed); } In the above example, an abstract class is declared with one implemented method and three unimplemented methods. A class inheriting from this class would have to implement the Wash, Rinse, and Spin methods. The following example shows what the implementation of this class might look like: Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 2 Abstract Classes ' Visual Basic Public Class MyWashingMachine Inherits WashingMachine Public Overrides Sub Wash() ' Wash code goes here End Sub Public Overrides Sub Rinse (loadSize as Integer) ' Rinse code goes here End Sub Public Overrides Function Spin (speed as Integer) as Long ' Spin code goes here End Sub End Class // C# class MyWashingMachine : WashingMachine { public MyWashingMachine() { // Initialization code goes here. } override public void Wash() { // Wash code goes here. } override public void Rinse(int loadSize) { // Rinse code goes here. } override public long Spin(int speed) { // Spin code goes here. } } When implementing an abstract class, you must implement each abstract (MustOverride) method in that class, and each implemented method must receive the same number and type of arguments, and have the same return value, as the method specified in the abstract class. Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 3 Abstract Classes Also, note that an abstract class does not mean that it should contain abstract members. Even we can have an abstract class only with non abstract members. For example: abstract class absClass { public void NonAbstractMethod() { Console.WriteLine("NonAbstract Method"); } } A sample program that explains abstract classes: using System; namespace abstractSample { //Creating an Abstract Class abstract class absClass { //A Non abstract method public int AddTwoNumbers(int Num1, int Num2) { return Num1 + Num2; } //An abstract method, to be //overridden in derived class public abstract int MultiplyTwoNumbers(int Num1, int Num2); } //A Child Class of absClass class absDerived:absClass { [STAThread] static void Main(string[] args) { //You can create an //instance of the derived class absDerived calculate = new absDerived(); int added = calculate.AddTwoNumbers(10,20); int multiplied = calculate.MultiplyTwoNumbers(10,20); Console.WriteLine("Added : {0}, Multiplied : {1}", added, multiplied); } Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 4 Abstract Classes //using override keyword, //implementing the abstract method //MultiplyTwoNumbers public override int MultiplyTwoNumbers(int Num1, int Num2) { return Num1 * Num2; } } } In the above sample, you can see that the abstract class absClass contains two methods AddTwoNumbers and MultiplyTwoNumbers. AddTwoNumbers is a non-abstract method which contains implementation and MultiplyTwoNumbers is an abstract method that does not contain implementation. The class absDerived is derived from absClass and the MultiplyTwoNumbers is implemented on absDerived. Within the Main, an instance (calculate) of the absDerived is created, and calls AddTwoNumbers and MultiplyTwoNumbers. You can derive an abstract class from another abstract class. In that case, in the child class it is optional to make the implementation of the abstract methods of the parent class. Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 5 Abstract Classes Example //Abstract Class1 abstract class absClass1 { public abstract int AddTwoNumbers(int Num1, int Num2); public abstract int MultiplyTwoNumbers(int Num1, int Num2); } //Abstract Class2 abstract class absClass2:absClass1 { //Implementing AddTwoNumbers public override int AddTwoNumbers(int Num1, int Num2) { return Num1+Num2; } } //Derived class from absClass2 class absDerived:absClass2 { //Implementing MultiplyTwoNumbers public override int MultiplyTwoNumbers(int Num1, int Num2) { return Num1*Num2; } } In the above example, absClass1 contains two abstract methods AddTwoNumbers and MultiplyTwoNumbers. The AddTwoNumbers is implemented in the derived class absClass2. The class absDerived is derived from absClass2 and the MultiplyTwoNumbers is implemented there. Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 6 Abstract Classes Important rules applied to abstract classes An abstract class cannot be a sealed class. I.e. the following declaration is incorrect. //Incorrect abstract sealed class absClass { } Declaration of abstract methods are only allowed in abstract classes. An abstract method cannot be private. //Incorrect private abstract int MultiplyTwoNumbers(); The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual. //Incorrect public abstract virtual int MultiplyTwoNumbers(); An abstract member cannot be static. //Incorrect publpublic abstract static int MultiplyTwoNumbers(); Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 7 Abstract Classes Abstract class vs. Interface An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class. An example of interface: interface iSampleInterface { //All methods are automaticall abstract int AddNumbers(int Num1, int Num2); int MultiplyNumbers(int Num1, int Num2); } Defining an abstract class with abstract members has the same effect to defining an interface. The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc. A class can inherit one or more interfaces, but only one abstract class. Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes. The selection of interface or abstract class depends on the need and design of your project. You can make an abstract class, interface or combination of both depending on your needs. Prepared by : Sehul Soni - E : sehulsoni@gmail.com M : +91-9925025622 Page 8