Abstract class with example

Abstract classes are classes which does not require any implementation . What ever be the methods written in the abstract clases it should be override in the derived class . The main point regarding the abstract class is that we can not create objects for the abstract classes . So the flow is like ,


1)  We are writing methods in the abstract classes. Should use the keyword Abstract for the class
      and should use virtual for the methods .

2)  If we need to do any operation by default whenever a method is calling, like that operations can be written 
      inside the virtual methods of the abstract classes .So that from some places suppose we are calling
      this      method  , automatically the common operations will occur and  we can add extra functionalities
      if   needed in the calling places.
 
  public class Class1:Abstct      {
           public override void save_name()
             {
                 base.save_name();
    
                  // here we can add extra operations if needed

             }
      }

The above one is the class which is inheriting the abstract class . Whenever the base will execute it will cause the operations under the virtual methods to be executed automatically without writing those operations again and again . 
   

  public abstract class Abstct      {
     public int name { get; set; }
     int age { get; set; }
     int id { get; set; }
         public virtual void save_name()
              {
                    //write code for saving any data this will
              }
        }

               This is the abstract class and the properties that we are writing inside this can be accessible in the inheriting classes only if it it's access modifier is public. The code for the saving will execute whenever the base. save_method will execute .

Class1 c1 = new Class1(); 

     
 Through this we can create object for the derived class . Suppose we need to access a property in the abstract class create an object for the derived class and objectname.propertyname can be use to access it  . Thats why we can access properties of derived class from the asp.net page .

Comments

Post a Comment

Popular posts from this blog

Introduction To Oracle10g

Insert

Except