Sunday, September 26, 2010

Dynamic Polymorphism: Abstract Classes and Interfaces


Dynamic Polymorphism:

Abstract Classes

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

1.Abstract class is a special kind of class that cannot be instantiated.
2.Abstract class can be only be inherited.
3.It is used to maintain the subclass in same hierarchies or standards.
4.Abstract class can have both abstract methods,virtual methods and non-abstract methods.
5.To define an abstract methods override keyword is used in the derived class.
6.Multiple inheritance of abstract classes is not possible.
7.Access Modifiers can be used in abstract classes.
8.Abstract classes can contain fields.
  
Interfaces:
           
            An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesnt support multiple inheritance, interfaces are used to implement multiple inheritance.
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp
{
    class interfaces
    {
        public  static void Main(string [] args)
        {
            implements obj = new implements();
            obj.add();
            obj.delete();
            obj.search();
            obj.update();
            Console.ReadLine();

        }
    }
    interface command
    {
        void add();
        void update();
        void delete();
        void search();

    }
    class implements : command
    {

        #region command Members

        public void add()
        {
            Console.WriteLine("Performs insertion operations");
        }

        public void update()
        {
            Console.WriteLine("Performs update operations");
        }

        public void delete()
        {
            Console.WriteLine("Performs delete operations");
        }

        public void search()
        {
            Console.WriteLine("Performs search operations");
        }

        #endregion
    }
}

Abstract Methods and Virtual Methods

abstract methods  are incomplete and they can only be defined in the inherited classes.Abstract methods can only be defined in the abstract classes.

The virtual function could be implemented by the inherited classees in their own way and the call to the method is decided at runtime. Virtual Methods can be overriden in the inherited classes.

Program 17:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp
{
    class AbstractClasses
    {
        public static void Main(string[] args)
        {
            cmmd obj = new cmmd();
            obj.add();
            obj.delete();
            obj.search();
            obj.update();
            Console.ReadLine();
        }
      
      
    }
    abstract class commands
    {
        public int a, b, res;
       
        public abstract void add();
            //uncommenting this lines generates an error;
        //{
        //    Console.WriteLine("The result operation is performed");
        //}
        public virtual void delete()
        {

        }
        public abstract void search();
       
        public void update()
        {
            Console.WriteLine("Performs update operations");
        }
    }
    class cmmd : commands
    {
        public override void add()
        {
            Console.WriteLine("Performs insertion operations");
        }

        public override void search()
        {
            Console.WriteLine("Performs search operations");
        }
        public override void delete()
        {
            Console.WriteLine("Performs delete operations");
        }
    }

}

No comments:

Post a Comment