Wednesday, November 12, 2008

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

namespace varun
{
    class inheritance_demo
    {
        public static void Main()
        {
           //entry point for program.
            //parent p_obj = new parent(); 
            //will not run, Error 1 Cannot create an instance of the abstract class or interface

            child c_obj = new child();
            c_obj.method_base();
            c_obj.override_to_implement_method();
            c_obj.must_override_method();
            c_obj.will_be_sealed_method();
            //all statements working fine without error.

        }
    
    }

     abstract class parent
    {
        public void method_base()
        {}
        public virtual void override_to_implement_method()
        {}

         // abstract method can only be declared in abstract classes only.
         //else error is generated
        public abstract void must_override_method();

         //this method will be sealed in derived class..
         //it cannot be sealed here.. if done..it will raise error
        //Error 1 'varun.parent.will_be_sealed_method()' cannot be sealed because it is not an override

        public  virtual void will_be_sealed_method() { }

       
       

    }

    class child : parent
    {
        //new defination of method_base..old defination is hidden now
        //, it is very useful when we have a third party class
        // and we cannot modify its code..we can define new for new defination of method

        public new void method_base()
        {
        }

        // virtual methods will either remain in base class or they must be
        //overridden to implement.there is no compulsion to override all virtual
        //methods.
        // new and override is allowed for virtual methods, no error generated.

        //public new void override_to_implement_method() { }
        //or
        public override void override_to_implement_method() { }


        // if not overidden the compiler is giving error.
        public override  void must_override_method() { }


        //sealing a method will prevent further inheritance..we cannot override
        //a sealed method.
        //A sealed keyword must be used with override .. means a 
        //method cannot be sealed in its base class it should be sealed in derived
        //class.. It also means that the base class method will be virtual.. becoz 
        //override is always used in conjunction with virtual.
        
      
       public sealed override void will_be_sealed_method() { }

   
     
    }
}